Sql:跨多个表的唯一字段约束(slug)(doctrine/symfony)
我有许多表将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于你在哪里发展。如果您正在维护该应用程序,那么我同意鲍勃·贾维斯的建议。看起来像是一堆互不相连的代码。
您所描述的是属于其自己的表中的实体。考虑到这一点,这就是我建模的方式。
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.
据我所知,您不能有跨表的唯一约束。处理该问题的一种方法是使用 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.