Hi all, how do I setup a relationship where there'...
# orm-help
n
Hi all, how do I setup a relationship where there's a
User
table and a
Transactions
table for storing transactions between users. The
Transactions
table has a from and to field , which should reference the id of the users sending and receiving on
User
table. How can 2 fields/columns reference the same key on another table. I can't do any of these.
Copy code
user  user  @relation(fields: [from, to], references :[id] )
user  user  @relation(fields: [from, to], references :[id, id] )
Please any help is greatly appreciated.
n
@Nathaniel Babalola 👋 Could you try something like this and see if it works?
Copy code
model User {
  id       Int           @id @default(autoincrement())
  name     String        @unique
  email    String        @unique
  Sender   Transaction[] @relation("sender")
  Receiver Transaction[] @relation("receiver")
}

model Transaction {
  id         Int  @id @default(autoincrement())
  from       User @relation("sender", fields: [senderId], references: [id])
  senderId   Int  @unique
  to         User @relation("receiver", fields: [receiverId], references: [id])
  receiverId Int  @unique
}
n
@Nurul thanks a lot , it worked
💯 1