Prisma ORM中@relation注解的[id]是否引用User模型的id字段?
Prisma关联关系中
references: [id]的指向说明 是的,你代码里user User @relation(fields: [userId], references: [id])中的[id]确实引用的是User模型中的id字段。
具体逻辑:
user User明确了该字段关联的目标模型是User;references: [id]指定了关联的目标字段为User模型的id;fields: [userId]则是当前Profile模型里存储外键的字段,它会保存关联的User的id值,配合@unique约束,和User模型里的profile Profile?形成一对一关联。
对应的模型代码:
model User { id Int @id @default(autoincrement()) posts Post[] profile Profile? } model Profile { id Int @id @default(autoincrement()) user User @relation(fields: [userId], references: [id]) // this references the id field in User model?? userId Int @unique // relation scalar field (used in the `@relation` attribute above) }
内容的提问来源于stack exchange,提问作者user3125823
相关产品推荐
相关产品推荐

