简体   繁体   English

我可以 map Prisma 中一对多关系中的两列吗?

[英]Can I map two columns in a one-to-many relationship in Prisma?

I'm trying to create a prisma schema, where I have an Order model, and a Client model. I would like to have a one-to-many relation between clients and orders.我正在尝试创建一个棱镜模式,其中我有一个订单 model 和一个客户 model。我想在客户和订单之间建立一对多关系。

I have it working with this Order model:我让它与这个订单 model 一起工作:

clientId     Client            @relation(fields: [clientId], references: [id])
clientId     Int

But wanted to also include the client name.但还想包括客户名称。 Something like this doesn't work:这样的事情不起作用:

client       Client            @relation(fields: [clientName, clientId], references: [name, id])
clientName   String
clientId     Int

This gives me the error这给了我错误

Error validating: The argument `references` must refer to a unique criteria in the related model `Client`. But it is referencing the following fields that are not a unique criteria: name, id

Is this something that is possible within the Prisma schema?这在 Prisma 模式中是可能的吗? I can do it with my controller in NestJS, but ideally I would want to be done in the Prisma layer.我可以在 NestJS 中使用我的 controller 来完成,但理想情况下我希望在 Prisma 层中完成。

I solved this by adding a @@unique([name, id]) constraint on the Client model. However, I'm not sure if this is the correct or best way to do this.我通过在客户端 model 上添加@@unique([name, id])约束来解决这个问题。但是,我不确定这是执行此操作的正确方法还是最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM