ActiveRecord HasMany/Belongs 可以使用非主键列作为关系吗?
我想要一个父子层次结构,并且我想要在父级上有“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
belongs_to 有一个选项可以让您指定关系:
There's an option for belongs_to that lets you specify the relationship: