ActiveRecord HasMany/Belongs 可以使用非主键列作为关系吗?

发布于 2024-08-12 05:01:10 字数 446 浏览 11 评论 0原文

我想要一个父子层次结构,并且我想要在父级上有“series-id”的概念。

因此,父级(如果您更新了它)将使旧的唯一密钥过期。保留系列 ID 并插入第二行。

我希望孩子们“挂”在该系列 ID 而不是唯一 ID 上。

您是否知道 BelongsTo 是否可以链接到非主键(但通过 GUID 唯一)列?

家长: UniqueId (guid)、SeriesId (guid)、名称、描述 孩子: UniqueId (guid)、ParentId(始终连接到上面的 UniqueId)、名称、描述。

我知道外键关系确实应该在关系的一侧有一个“键”,但是,根据我的 GUID,它们是类似键的......但不是真正的键。

这没什么大不了的,但如果我通过更新其元数据来“过期”父级,我想避免必须将所有子级“复制”到父级的新副本中。

我是否正在尝试做一些不可能发生的事情?

I'd like to have a parent-child hierarchy AND I'd like to have the concept of a "series-id" on the parent.

So the parent (if you updated it) would expire the old unique key.. keep the series id and insert a second row.

I'd like the children to "hang" off that series id rather than the unique id.

Do you know if it's possible for BelongsTo to link into a non primary key (yet unique via GUIDs) column?

Parent:
UniqueId (guid), SeriesId (guid), Name, Description
Child:
UniqueId (guid), ParentId (always connects ot UniqueId-Above), Name, Description.

I know that foreign key relations really should have a "key" on one side of the relation however, by my GUIDs they are key-like.. but not really keys.

Its not a big deal but if I "expire" the parent by updating it's metadata i'd like to avoid having to "copy" all the children into the new copy of the parent.

Am I trying to do something that can't happen?

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

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

发布评论

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

评论(1

救星 2024-08-19 05:01:10

belongs_to 有一个选项可以让您指定关系:

:foreign_key
    Specify the foreign key used for the association. By default this is guessed to be the name of the association with an "_id" suffix. So a class that defines a belongs_to :person association will use "person_id" as the default :foreign_key. Similarly, belongs_to :favorite_person, :class_name => "Person" will use a foreign key of "favorite_person_id".

There's an option for belongs_to that lets you specify the relationship:

:foreign_key
    Specify the foreign key used for the association. By default this is guessed to be the name of the association with an "_id" suffix. So a class that defines a belongs_to :person association will use "person_id" as the default :foreign_key. Similarly, belongs_to :favorite_person, :class_name => "Person" will use a foreign key of "favorite_person_id".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文