0

I'm trying to create a many-to-many relationship between two models: Entity and Taransaction


model Entity {
  id String @id @default(cuid())
  name String

  purchases Transactions[] // references the source field on Transaction
  sales Transaction[] // references the destination field on Transaction
}

model Transaction {
  source Entity
  destination Entity
  amount Float
  date DateTime @default(now())
}

What I want is to be able to retrieve all the Entity's purchases which points to the source of the transaction and all the Entity's sales which points to the destination of the transaction.

My question is, what would the schema for this relation would look like using Prisma 2?

brielov
  • 1,887
  • 20
  • 28

1 Answers1

1

Could you try this:

model Entity {
  id   String @id @default(cuid())
  name String

  purchases Transact[] @relation("purchases")
  sales     Transact[] @relation("sales")
}

model Transact {
  id            String   @id @default(cuid())
  source        Entity   @relation("purchases", fields: [sourceId], references: [id])
  destination   Entity   @relation("sales", fields: [destinationId], references: [id])
  amount        Float
  date          DateTime @default(now())
  sourceId      String
  destinationId String
}

Ryan
  • 5,229
  • 1
  • 20
  • 31