Postgres中的部分唯一索引以确保存在满足约束的行?

发布于 2025-01-19 13:57:29 字数 202 浏览 3 评论 0原文

我有一个用户表、一个团队表和一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十秒萌定你 2025-01-26 13:57:29

NOT NULL 外键添加到指向用户默认团队的 users 表中。

Add a foreign key that is NOT NULL to the users table that points to the default team of the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文