复合主键可以用来防止链接不相关的对象吗?
如果我有几个模型(说出您最喜欢的框架):
# pseudo-code
class Fruit
primary_key = CompositeKey(tenant_id, object_id)
weight = DecimalField()
class Restaurant
primary_key = CompositeKey(tenant_id, object_id)
haz_cheeseburger = BooleanField()
fruit_available = ForeignKey(Fruit) # <-- important part
是否可能存在数据库约束,导致无法插入属于 tenant_id
部分的 Resaturant
主键指向 TenantA
,谁的 fruit_available
的 tenant_id
指向 TenantB
。基本上,防止我意外地将属于不同租户的对象关联起来。
If I have a couple models in (name your favorite framework):
# pseudo-code
class Fruit
primary_key = CompositeKey(tenant_id, object_id)
weight = DecimalField()
class Restaurant
primary_key = CompositeKey(tenant_id, object_id)
haz_cheeseburger = BooleanField()
fruit_available = ForeignKey(Fruit) # <-- important part
Is it possible to have a database constraint that will make it impossible to insert a Resaturant
who's tenant_id
part of the primary key points to TenantA
and who's fruit_available
's tenant_id
points to TenantB
. Basically, protect me from accidentally relating objects that belong to different tenants.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您也许可以设置外键。在餐厅表中,
这部分取决于您的 dbms,部分取决于
fruit (tenant_id,fruit_available)
上是否存在任何类型的唯一约束。但这并不能阻止您使用错误的tenant_id作为Restaurant主键的一部分。
If I've understood you correctly, you might be able to set a foreign key. In the restaurant table,
It depends in part on your dbms, and in part on whether there's a unique constraint of any kind on
fruit (tenant_id, fruit_available)
.That won't prevent you from using the wrong tenant_id as part of Restaurant's primary key, though.