我在理解 Prisma 中的关系和数据建模时遇到问题。
我有以下数据模型,其中一个用户可以拥有许多房屋,一个用户也可以住在许多房屋中。所以我有:
model Home {
id String @id @default(cuid())
User User? @relation(name: "HomeUser", fields: [userId], references: [id])
userId String?
owner User @relation(name: "HomeOwner", fields: [ownerId], references: [id])
ownerId String
}
model User {
id String @id @default(cuid())
listedHomes Home[] @relation("HomeOwner")
}
它给了我这个错误:
Error validating field `User` in model `Home`: The relation field `User` on model `Home` is missing an opposite relation field on the model `User`
我该如何解决这个问题?提前致谢。
尝试添加您的
listedHome
关系name
关键字,如下所示。
此外,小写
u
的Home.User
以保持一致性(或全部大写。您决定)
model Home {
id String @id @default(cuid())
user User? @relation(name: "HomeUser", fields: [userId], references: [id])
userId String?
owner User @relation(name: "HomeOwner", fields: [ownerId], references: [id])
ownerId String
}
model User {
id String @id @default(cuid())
listedHomes Home[] @relation(name: "HomeOwner")
}