has_one 和 has_many 表的混淆

发布于 2024-09-27 18:24:49 字数 1082 浏览 1 评论 0原文

在 Rails ActiveRecord Associations 指南中,我对为什么 has_one< 的表感到困惑/code> 和 has_many 相同:

< 的示例表code>has_many:

customers(id,name)
orders(id,customer_id,order_date)

has_one 的示例表这些表在数据库级别也允许供应商拥有多个帐户,但我们只希望每个供应商有一个帐户

suppliers(id,name)
accounts(id,supplier_id,account_number) #Foreign Key to supplier here??

has_one 的表不应该是这样的吗相反:

suppliers(id,name,account_id) #Foreign Key to account here
accounts(id,account_number)

现在,由于 account_id 位于供应商表中,因此供应商永远不能拥有多个帐户。

Rails 指南中的示例是否不正确?

或者,Rails 是否使用 has_many 类型的方法,但限制 many 部分的发生?

In the Rails ActiveRecord Associations guide, I'm confused over why the tables for has_one and has_many are identical:

Example tables for has_many:

customers(id,name)
orders(id,customer_id,order_date)

Example tables for has_one:
these tables will, at the database level, also allow a supplier to have many accounts, but we just want one account per supplier

suppliers(id,name)
accounts(id,supplier_id,account_number) #Foreign Key to supplier here??

Shouldn't the tables for has_one be like this instead:

suppliers(id,name,account_id) #Foreign Key to account here
accounts(id,account_number)

Now because the account_id is in the suppliers table, a supplier can never have more than one account.

Is the example in the Rails Guide incorrect?

Or, does Rails use the has_many kind of approach but restricts the many part from happening?

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

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

发布评论

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

评论(3

三五鸿雁 2024-10-04 18:24:49

如果你这样想——它们都是一样的:

1 个客户可以有多个订单,因此每个订单记录都指向客户。

1个供应商可以有一个账户,这是“有多个”的一种特例,因此它与指向供应商的账户同样有效。

多对多的情况也是一样,联结表指向各个记录...(如果一个学生可以选多门课,一个班可以有很多学生,那么招生表就指向回学生和班级记录)。

至于为什么帐户指向供应商与帐户指向供应商,我不完全确定我们是否可以采用任何一种方式,或者一种形式比另一种更好。

If you think about this way -- they are all the same:

1 customer can have many orders, so each order record points back to customer.

1 supplier can have one account, and it is a special case of "has many", so it equally works with account pointing back to supplier.

and it is the same case with many-to-many, with junction table pointing back to the individual records... (if a student can take many classes, and one class can have many students, then the enrollment table points back to the student and class records).

as to why account points back to supplier vs account points to supplier, that one I am not entirely sure whether we can have it either way, or one form is better than the other.

箜明 2024-10-04 18:24:49

我相信这与限制有关。使用 has_one,rails 将尝试强制每个供应商只有一个帐户。但是,使用 has_many 时,不会强制执行任何约束,因此具有 has_many 的供应商将被允许存在多个帐户。

在考虑 Rails 中的关系及其创建时,确实需要一些时间来适应。如果您想在数据库端强制使用外键(因为 Rails 不会在应用程序层之外执行此操作),请查看 马修希金斯的外国人

I believe it has to do with the constraints. With has_one rails will try to enforce that there is only one account per supplier. However, with a has_many, there will be no constraint enforced, so a supplier with has_many would be allowed to exist with multiple accounts.

It does take some getting used to when thinking about the relationships and their creation in rails. If you want to enforce foreign keys on the database side (since rails doesn't do this outside of the application layer), take a look at Mathew Higgins' foreigner

独自唱情﹋歌 2024-10-04 18:24:49

如果我正确理解您的问题,您认为 has_one/belongs_to 关系中存在双向 1:1 关系。这并不完全正确。您可以:

Class Account
  belongs_to :supplier
  belongs_to :wholesaler
  belongs_to :shipper
  # ...
end

account = supplier.account       # Get supplier's account
wholesaler = Wholesaler.new
wholesaler.accounts << account   # Tell wholesaler this is one of their suppliers
wholesaler.save

我并不是说您的应用程序实际上以这种方式运行,但您可以看到“属于”另一个模型的表(不,我们说一个模型)如何不排除属于任意数量的模型。正确的?所以这种关系确实是无限的:1。

我应该补充一点,has_one 实际上是 has_many 的退化情况,只是添加了单一化关联的语法糖和其他一些缺陷。除此之外,它们几乎是同一件事,这也是它们看起来很相似的原因。

If I understand your question correctly, you believe there is a 1:1 relationship bi-directionally in a has_one/belongs_to relationship. That's not exactly true. You could have:

Class Account
  belongs_to :supplier
  belongs_to :wholesaler
  belongs_to :shipper
  # ...
end

account = supplier.account       # Get supplier's account
wholesaler = Wholesaler.new
wholesaler.accounts << account   # Tell wholesaler this is one of their suppliers
wholesaler.save

I'm not saying your app actually behaves this way, but you can see how a table -- no, let's say a model -- that "belongs to" another model is not precluded from belonging to any number of models. Right? So the relationship is really infinity:1.

I should add that has_one is really a degenerate case of has_many and just adds syntactic sugar of singularizing the association and a few other nits. Otherwise, it's pretty much the same thing and it's pretty much why they look alike.

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