Sql:跨多个表的唯一字段约束(slug)(doctrine/symfony)

发布于 2024-09-06 16:20:14 字数 182 浏览 1 评论 0原文

我有许多表将使用 url 'slug' 进行查找。例如,“新闻”和“博客”都具有“slug”字段,该字段被定义为学说数据库模式中的唯一字段。

有什么方法可以在两个表中扩展这种唯一性:例如,如果有一篇新闻文章的 slug=“good-story”,那么如果我尝试将其作为博客文章的 slug 输入,它将失败?

谢谢 汤姆

I have a number of tables that will be looked up using the url 'slug'. For example 'news' and 'blog' both have the 'slug' field, which is defined as a unique field in the doctrine database schema.

Is there any way I can extend this uniqueness across both tables: for example if there is a news article with slug= "good-story" then it will fail if I try to enter that as a slug for a blog article?

Thanks
Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

无妨# 2024-09-13 16:20:14

取决于你在哪里发展。如果您正在维护该应用程序,那么我同意鲍勃·贾维斯的建议。看起来像是一堆互不相连的代码。

您所描述的是属于其自己的表中的实体。考虑到这一点,这就是我建模的方式。

tbl_slug
--------------------
slug
slug_type
-primary key ( slug, slug_type )
-constraint - slug is unique to table


tbl_news
--------------------
id
fk_slug_slug
fk_slug_type
-constraint - fk_slug_type = 'news'


tbl_blog
--------------------
id
fk_slug_slug
fk_slug_type
-constraint - fk_slug_type = 'blog'

Depending on where you are in development. If you are maintaining the app, then I agree with Bob Jarvis's suggestion. Seems like a heap of disconnected code.

What you are describing is an entity that belongs in its own table. With that in mind, this is how I might model it.

tbl_slug
--------------------
slug
slug_type
-primary key ( slug, slug_type )
-constraint - slug is unique to table


tbl_news
--------------------
id
fk_slug_slug
fk_slug_type
-constraint - fk_slug_type = 'news'


tbl_blog
--------------------
id
fk_slug_slug
fk_slug_type
-constraint - fk_slug_type = 'blog'

又爬满兰若 2024-09-13 16:20:14

据我所知,您不能有跨表的唯一约束。处理该问题的一种方法是使用 SLUG 表,其中“slug”字段为主键。您需要在“新闻”和“博客”上使用 INSERT 触发器,以尝试将新的“slug”插入 SLUG 表中。如果“news.slug”或“blog.slug”已更新,您可能还想在“news”和“blog”上放置 UPDATE 触发器来更新 SLUG.slug。

我希望这有帮助。

As far as I'm aware you can't have a UNIQUE contstraint which spans tables. One way to handle it would be to have a SLUG table where the 'slug' field would be the primary key. You'd need INSERT triggers on 'news' and 'blog' which would try to insert the new 'slug' into the SLUG table. You might also want to put UPDATE triggers on 'news' and 'blog' to update SLUG.slug if 'news.slug' or 'blog.slug' was updated.

I hope this helps.

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