问题描述
这是我正在开发的应用程序的架构
user
id
...
product
id
userId --> foreign key,references user(id)
name
general_fields...
product_type1:
id --> foreign key,references product(id)
product_type1_fields...
product_type2:
id --> foreign key,references product(id)
product_type2_fields...
如你所见,
- 我们有不同类型的产品
- 所有产品都有一些共同的字段,例如
name
- 并且每种类型都有自己的字段
我希望一个用户有多个产品类型 2 的实例,但只有一个产品类型 1 的实例。如何在数据库层实现这种约束?
即
我想到的第一件事是将 unique
userId 字段添加到 product type1 中,这样就不会将重复的 userId 插入到 product_type1 表中,但我们已经在 userId
的父表中有 type1
(这是产品表)所以它可能不是最好的解决方案。
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)