设置博客数据模型的最佳实践

发布于 2024-07-19 05:33:45 字数 421 浏览 5 评论 0原文

我的博客数据模型有以下设置:

Blog               Articles             Categories
-----              --------          1  ----------
ID  \ 1         *  ID            /----- ID
Name \------------ BlogID       /       Name 
Owner              Name      * /      
                   CategoryID-/

我担心此数据模型如何与博客的“最佳实践”相匹配。 如果删除博客,是否应该删除该博客的所有文章? 如果删除类别,文章会发生什么情况?

有没有更好的办法? 我有什么遗漏的吗? “最佳实践”建议做什么?

I have the following setup for my Blog Data Model:

Blog               Articles             Categories
-----              --------          1  ----------
ID  \ 1         *  ID            /----- ID
Name \------------ BlogID       /       Name 
Owner              Name      * /      
                   CategoryID-/

I'm worried about how this data model matches the 'best practices' for Blogs. If a Blog is deleted, should all the articles for that blog be deleted? If a Category is deleted, what should happen to the Articles?

Is there a better way? Is there something I'm missing? What do 'best-practices' suggest to do?

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

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

发布评论

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

评论(7

命硬 2024-07-26 05:33:45

这实际上取决于您的用例。 许多博客应用程序使用具有多对多关系的标签,而不是具有一对多关系的类别,因为将一篇文章视为与(可能)许多标签相关比将一篇文章视为“更有意义”包含”在一个类别中。

如果你的博客的逻辑是一篇文章严格包含在一个类别中,那么一对多的关系是合理的。 在某些情况下,级联删除也是合理的。 毕竟,那些孤儿文章到底在哪里呢?

这只是您必须预测用户群直觉的情况。 他们会认为什么是自然的。

顺便说一句,这需要更多的工作,但如果是我,我不会进行级联删除。 我会将文章标记为孤立文章,并在大多数视图中隐藏它们。 我还将提供一些用于恢复孤立文章的界面。 我会考虑提供一个脚本来删除任何已经孤立超过一段时间的东西。 这样,如果您对用户期望发生的情况有误,您可以解决问题。

This really depends on your use case. A lot of blog applications use tags with a many-to-many relationship instead of categories with a one-to-many relationsip because it makes more sense to see an article as related to (possibly) many tags than to see an article as "contained" within a category.

If the logic of your blog is that an article is strictly contained in a category, then the one-to-many relationship is justified. And in some circumstances a cascading delete would also be justified. After all, where would the orphaned articles even exist.

This is just a case where you must anticipate your user-base's intuition. What would they assume is natural.

Incidentally, it's a lot more work, but if it were me, I wouldn't do a cascading delete. I would mark articles as orphaned and hide them from most views. I would also provide some interface for restoring orphaned articles. And I would consider providing a script that deletes anything that's been orphaned for more than some period of time. That way, if you were wrong about what your users expected to happen, you can fix the problem.

下壹個目標 2024-07-26 05:33:45

这篇(较旧的)帖子介绍了一些标签相关的数据库模式变体,包括详细的讨论。 也许这是一个很好的起点。

This (older) post introduces some tag related db schema variations including a detailed discussion. Maybe it's a good starting point.

黯淡〆 2024-07-26 05:33:45

依靠。 如果您的博客引擎不允许没有指定类别的条目,您可能应该通知用户后果并删除文章。 否则,您可以使用 ON DELETE SET NULL 代替。

Depends. If your blog engine does not allow an entry without an specified category, you should probably notify the user of the consequence and delete the articles. Otherwise you could use an ON DELETE SET NULL instead.

夜唯美灬不弃 2024-07-26 05:33:45

如果您的关系设置正确,则在不先删除所有相关数据的情况下,您应该无法删除该类别。 删除一个类别会删除一堆文章,这看起来确实很奇怪,但同时,如果一个类别不再有效,那么这些文章也不应该有效。

也许可以选择将所有文章重新映射到新类别。

If your relationships are set up right, you shouldn't be able to delete the category without first deleting all related data. It does seem weird that deleting a category would remove a bunch of articles, but at the same time, if a category is no longer valid, then the articles shouldn't be either.

Perhaps an option to remap all articles to a new category.

娇俏 2024-07-26 05:33:45

有几个问题。 一篇文章可以有多个类别吗? 您是否允许文章有 0 个与之关联的类别? 但根据您的描述,我想说您不想删除这些文章。 根据我开发博客时的经验,类别通常与文章/帖子是多对多的关系(多个类别描述多个帖子)。 所以我不会删除它们,但这又取决于您的业务逻辑以及您想要如何处理它。

A couple of questions. Can an article have more than one category? Are you allowing articles to have 0 categories associated with them? By your description though, I'd say that you don't want to delete the articles. The categories, in my experience when developing a blog, are usually a many to many relationship with articles/posts (multiple categories describing multiple posts). So I wouldn't delete them, but once again it would depend on your business logic and how you wanted to handle it.

小糖芽 2024-07-26 05:33:45

那么,将博客类别与博客文章相关联的外键(应该存在)不允许您这样做。 如果您想保持文章和引用完整性,您应该考虑将文章类别更改为其他类别。

Well, your foreign key (which should exist) relating your blog category to your blog post wouldn't allow you to. You should consider change the category of your article to a different one if you want to keep your article and referential integrity.

我三岁 2024-07-26 05:33:45

忘记数据模型,使用对象模型,即数据是实现,对象是接口,这更接近需求。

Forget about data model , use Object Model, that is what data is implementation, and object is interface, which is closer to the requirement.

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