![](/img/trans.png)
[英]How to query two collections that have a “one-to-many” relationship in MongoDB?
[英]Can I map two columns in a one-to-many relationship in Prisma?
我正在嘗試創建一個棱鏡模式,其中我有一個訂單 model 和一個客戶 model。我想在客戶和訂單之間建立一對多關系。
我讓它與這個訂單 model 一起工作:
clientId Client @relation(fields: [clientId], references: [id])
clientId Int
但還想包括客戶名稱。 這樣的事情不起作用:
client Client @relation(fields: [clientName, clientId], references: [name, id])
clientName String
clientId Int
這給了我錯誤
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
這在 Prisma 模式中是可能的嗎? 我可以在 NestJS 中使用我的 controller 來完成,但理想情況下我希望在 Prisma 層中完成。
我通過在客戶端 model 上添加@@unique([name, id])
約束來解決這個問題。但是,我不確定這是執行此操作的正確方法還是最佳方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.