Postgres中的部分唯一索引以确保存在满足约束的行?
我有一个用户表、一个团队表和一个users_teams 表。
我正在 postgres 中寻找一种方法来执行以下操作:
我想确保用户始终有一个默认团队,即 users_teams 表中 is_default true 的记录。
我知道我可以使用部分唯一索引来确保用户没有超过 1 个默认团队,所以我想知道是否可以达到相同的效果。
谢谢
I have a users table, a teams table and a users_teams table.
I am looking for a way in postgres to do the following:
I want to ensure that a user always has a default team, a record with is_default true in the users_teams table.
I know I can use partial unique index for making sure a user does not have more than 1 default team, so I am wondering if something of the same effect is possible.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
NOT NULL
外键添加到指向用户默认团队的users
表中。Add a foreign key that is
NOT NULL
to theusers
table that points to the default team of the user.