如何仅在Prisma中不是整个物体中的外键
我刚刚开始学习Nestjs,我正在使用Prisma作为我的ORM,我创建了两个模型(员工,客户),这些模型将包含外国钥匙,客户将拥有员工ID,反之亦然(一个 - 一对一的关系),但是正如您将在模型中看到的那样,员工将包含整个客户端的“对象”,但是我想要的是他的客户ID作为独立列:
这是模型:
model Employe {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updated DateTime @updatedAt
email String @unique
hash String
nom String
prenom String
// I want the client ID here too
client Client?
@@map("employes")
}
model Client {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updated DateTime @updatedAt
email String @unique
hash String
nom String
prenom String
EmpId Int? @unique
emp Employe? @relation(fields: [EmpId], references: [id], onDelete: Cascade, onUpdate: Cascade)
@@map("clients")
}
那么有什么方法?
- 框架:Nestjs
- Orm:Prisma
- DB:PostgreSQL
- 平台:Docker
I've just started learning nestjs and I'm using Prisma as my ORM, I've created two models (Employees, Clients), these models will contain foreign keys , the client will have the employee id and vice versa (a one-to-one relation), but as you will see in the models the employee will contain the whole Client "object", but what I want also is his client's id as an independent column :
this is the models:
model Employe {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updated DateTime @updatedAt
email String @unique
hash String
nom String
prenom String
// I want the client ID here too
client Client?
@@map("employes")
}
model Client {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updated DateTime @updatedAt
email String @unique
hash String
nom String
prenom String
EmpId Int? @unique
emp Employe? @relation(fields: [EmpId], references: [id], onDelete: Cascade, onUpdate: Cascade)
@@map("clients")
}
So is there any way ?
- Framework: NestJS
- ORM: Prisma
- DB : Postgresql
- Platform: Docker
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
客户端模型中的字段
emp
client
在雇员模型中是关系字段,因此它们在数据库中不存在,它们只是在Prisma级别。您只需要在关系的一侧定义外键 - 一对一的关系prisma参考
您可以将客户端存储在雇员上也是模型。
The fields
emp
in the Client model andclient
in Employe model are relation fields so they won't exist in the database, they are just at the prisma level.You only need to define the foreign key on one side of the relation - One to One relation Prisma reference
You could store clientId on the Employe model as well.
在此链接中,有一个示例可以为您提供帮助:
https://www.prisma.io/docs/guides/general-guides/database-workflows/foreign-keys/postgresql
In this link there is an example that can help you:
https://www.prisma.io/docs/guides/general-guides/database-workflows/foreign-keys/postgresql
我或多或少理解你的意思。我不明白为什么您不设置关系,但是在创建客户或雇员时可以使用此结构。
创建雇员
创建客户
I more or less understand what you mean. I don't understand why you don't set relation, but you can use this structure when creating a client or employe.
to create the employe
to create the client