gpt4 book ai didi

mysql - 棱镜数据模型 : Primary key as a combination of two relational models

转载 作者:行者123 更新时间:2023-11-29 01:35:06 25 4
gpt4 key购买 nike

我在 Prisma 数据建模中遇到问题,我必须限制用户只能为产品提交一条评论。我有针对非约束情况的以下设计

Should Customer and Product be combined into a primary key in ProductReview model, or should this constraint be imposed at the application server level, and not at the database level?

现在的数据模型(非约束版本):

type Product {
id: ID! @unique
title: String!
reviews: [ProductReview!]! @relation(name: "ProductReviews", onDelete: CASCADE)
}

type Customer {
id: ID! @unique
email: String @unique
}

type ProductReview {
id: ID! @unique
forProduct: Product! @relation(name: "ProductReviews", onDelete: SET_NULL)
byCustomer: Customer!
review: String!
ratinng: Float!
}

最佳答案

看起来 Prisma v2 引入了复合主键:

https://newreleases.io/project/github/prisma/prisma/release/2.0.0-preview023

该链接的示例:

model User {
firstName String
lastName String
email String

@@id([firstName, lastName])
}

所以在给定的问题示例中,应该可以添加到ProductReview:

@@id([id, forProduct])

关于mysql - 棱镜数据模型 : Primary key as a combination of two relational models,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53373101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com