model User {
id String @id @default(cuid())
posts Post[]
checkedPosts Post[]
}
model Post {
id String @id @default(cuid())
authorId String
checkedById String
user User @relation(fields: [authorId], references: [id], onDelete: SetNull)
checkedBy User @relation(fields: [checkedById], references: [id], onDelete: SetNull)
}
リレーション元とリレーション先が曖昧になってしまうためです。
リレーションの両側にnameを付ける
以下のようにリレーションの両側の@relationに同じ名前をつけてあげるとうまく機能します。
model User {
id String @id @default(cuid())
posts Post[] @relation("Author")
checkedPosts Post[] @relation("CheckedPosts")
}
model Post {
id String @id @default(cuid())
checkedById String
authorId String
user User @relation("Author", fields: [authorId], references: [id], onDelete: SetNull)
checkedBy User @relation("CheckedPosts", fields: [payerId], references: [id], onDelete: SetNull)
}
これでリレーションの曖昧がなくなって機能します。
参考
あわせて読みたい
Relations (Reference) | Prisma DocumentationA 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 …
コメント