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)
}
これでリレーションの曖昧がなくなって機能します。
参考
Prisma
Relational data modeling in Prisma ORMHow to model one-to-one, one-to-many, many-to-many, and polymorphic relations in Prisma ORM for PostgreSQL, including which side owns the foreign key.
コメント