数据模型,多对多与多对一关系

发布于 2024-09-28 19:53:24 字数 249 浏览 8 评论 0原文

我有两种类型的帐户(客户和提供商),我选择单表策略进行持久化。客户创建订单(one2many),提供商以拍卖方式对订单进行投标(多对多关系,因为他可以对许多订单以及其他提供商进行投标)。我的问题是,是否有可能同时拥有这些关系?因为从逻辑上讲它是可行的。但 MDA 代码生成器不喜欢它。如果是这样,我可能会遇到这个数据模型的缺点。

提前致谢。

替代文本

I have two types of accounts (customer and provider), I chose the single-table strategy for persistence. Customer creates Orders (one2many) and provider bids on the orders in auction style (many2many relationship, because he can bid on many orders as well as other providers). My question is, is it possible to have these relationships simultaneously ? Because logically it can work. But MDA code generators don't like it. If so, what drawbacks I could come across with this datamodel.

Thanks in advance.

alt text

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

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

发布评论

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

评论(2

信愁 2024-10-05 19:53:24

缺点是您无法在数据库中强制帐户表中的 accountID 和出价表中的 accountID 之间的引用完整性(我假设它代表对订单出价的提供商的 accountID)因为并非所有 accountID 值都是允许的。

但是,不要放弃帐户的单表解决方案,这很可能是解决您问题的正确方案(我不能肯定地说不完全理解提供商和客户之间的关系)。要两者使用单表解决方案并允许引用完整性,您需要执行以下操作:

  1. 从帐户中删除 isProvider 和 isCustomer。

  2. 添加两个新表“提供商”和“客户”。每个表都有一个 accountID 列,它既是该表中的主键,又是返回原始帐户表的外键。

  3. 将提供商或客户特有的帐户中的任何其他列迁移到相应的表中。

  4. 现在,订单表中的 accountID 应该是客户的外键,而不是帐户的外键。同样,Bids 中的 accountID 列成为 Providers 而不是 Accounts 的外键。

提供了帐户的关系完整性和单表存储。

The disadvantage is that you can't enforce referential integrity in the database between the accountID in the accounts table and the accountID in the bids table (which I assume represents the accountID of the provider bidding on the order) because not all accountID values are allowable.

But, don't give up on the single-table solution for accounts, which may well be the correct one for your problem (I can't say for sure not completely understand the relation between providers and customers). Here's what you need to do to both use the single table solution and allow referential integrity:

  1. Remove isProvider and isCustomer from Accounts.

  2. Add two new tables Providers and Customers. Each table will have an accountID column which is both the primary key in that table and a foreign key back to the original account table.

  3. Migrate any additional columns from Accounts that are unique to either Providers or Customers into the appropriate table.

  4. Now, the accountID in the Orders table should be a foreign key into Customers, not Accounts. Similarly, the accountID column in Bids becomes a foreign key into Providers rather than Accounts.

Relational integrity and single-table storage for accounts is provided for.

长途伴 2024-10-05 19:53:24

“我选择单表策略是为了持久化”——在我看来,这实际上并不是将它们结合起来的好理由。客户和提供商是根本不同的动物。

您遇到麻烦的事实清楚地表明您很可能以错误的方式做事 - IT 行业中的大多数事情都是如此(可能还有生活本身,但您不需要我在这方面劝说)。

我会将它们分成不同的表来解决这个特定的问题。

如果您确实希望共享部分数据,您可以将常见的内容放在另一个表中,并从客户和提供商表中引用它。

如果单个实体既可以是客户又可以是提供商,您可能需要这样做 - 在这种情况下,您可能希望两个不同的表条目共享相同的信息(例如余额、声誉等)。

"I chose the single-table strategy for persistence" - that's actually not that good a reason for combining them, in my opinion. Customers and providers are fundamentally different beasts.

The fact that you're having troubles is a clear indication that you're most likely doing it the wrong way - that's true of most things in the IT industry (and probably life itself but you don't need me proselytising on that).

I would separate them out into different tables to resolve this particular problem.

If you really want part of the data to be shared, you could put the common things in yet another table and reference it from the customers and providers tables.

You may want this if a single entity can be both a customer and provider - in that case, you would want the two different table entries to share the same information (such as balance, reputation and so on).

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