Prisma 2 relations to the same model

Hi, I'm working with Prisma for the first time, and I'm having trouble with a relation: I have a Transfer and a Deposit model, but the Transfer has a from and a to relation both relating to the Deposit how can I implement this? schema.prisma:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
deposits Deposit[]
labels Label[]
expenses Expense[]
revenues Revenue[]
transfers Transfer[]
}

model Deposit {
id String @id @default(cuid())
uerId String
deposit_type depositTypes
name String
description String?
balance Float
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
user User @relation(fields: [uerId], references: [id], onDelete: Cascade)
expenses Expense[]
revenues Revenue[]
Transfer Transfer[] // from ???
Transfer Transfer[] // to ???
}

model Transfer {
id String @id @default(cuid())
uerId String
from_Deposit_id String
to_Deposit_id String
label_ids Label[]
name String
description String?
amount Float
fee Float
date DateTime
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
user User @relation(fields: [uerId], references: [id], onDelete: Cascade)
from_Deposit Deposit @relation(fields: [from_Deposit_id], references: [id])
to_Deposit Deposit @relation(fields: [to_Deposit_id], references: [id])
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
deposits Deposit[]
labels Label[]
expenses Expense[]
revenues Revenue[]
transfers Transfer[]
}

model Deposit {
id String @id @default(cuid())
uerId String
deposit_type depositTypes
name String
description String?
balance Float
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
user User @relation(fields: [uerId], references: [id], onDelete: Cascade)
expenses Expense[]
revenues Revenue[]
Transfer Transfer[] // from ???
Transfer Transfer[] // to ???
}

model Transfer {
id String @id @default(cuid())
uerId String
from_Deposit_id String
to_Deposit_id String
label_ids Label[]
name String
description String?
amount Float
fee Float
date DateTime
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
user User @relation(fields: [uerId], references: [id], onDelete: Cascade)
from_Deposit Deposit @relation(fields: [from_Deposit_id], references: [id])
to_Deposit Deposit @relation(fields: [to_Deposit_id], references: [id])
}
full schema.prisma: https://pastecord.com/upuxegexeg.prisma
3 Replies
JulieCezar
JulieCezar2y ago
Prisma
Relations (Reference)
A relation is a connection between two models in the Prisma schema. This page explains how you can define one-to-one, one-to-many and many-to-many relations in Prisma.
JulieCezar
JulieCezar2y ago
TLDR; you need to define names for each of the relations and reference them
Ayato
Ayato2y ago
Thanks
Want results from more Discord servers?
Add your server