在错误/错误状态模型中,外键将驻留在哪里?
我总是被外键困住,所以我有一个问题,外键应该驻留在错误跟踪系统中的哪里,其中错误在任何给定时间都具有单一状态,而只存在少量状态(开放、正在调查、已解决,待批准)。 所以每个状态都有很多与之相关的错误。 我的假设是外键应该作为引用 Status 表中 id 列的 status_id 列驻留在 Bug 表中。 这是一个安全的假设吗?
TABLE:
Bug
id integer
desc string
status_id integer fk
Status
id integer
desc string
RAILS MODEL:
Bug
has_one :status
Status
has_and_belongs_to_many :bugs
I always get stuck on foreign keys so I have a question about where the foreign key should reside in a bug tracking system in which a bug has a single status at any given time while only a small number of statuses exist (Open, Under Investigation, Resolved, Pending Approval). So each status has many bugs associated with it. My assumption is the foreign key should reside in the Bug table as a status_id column referencing the id column in the Status table. Is this a safe assumption?
TABLE:
Bug
id integer
desc string
status_id integer fk
Status
id integer
desc string
RAILS MODEL:
Bug
has_one :status
Status
has_and_belongs_to_many :bugs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,你的假设是正确的。 只要每个错误只有一个状态,您就可以只包含该表的外键。
Yes you are correct in that assumption. As long as each Bug will only have a single Status, you can just include the foreign key to that table.
您的假设是正确的,更重要的是关系(一对多/一对一/多对多)决定哪个表是主键表,哪个是外键表?
在这种情况下,状态表显然包含这里 FK 关系的主键。 如果是相反的情况,那么每个状态都必须先存在于 Bug 表中,然后才能存在于 Status 表中,这显然不是有意的。
You're correct in your assumption, more importantly then the relationship (One-Many/One-One/Many-Many) is deciding which table is the Primary Key table and which is the Foreign Key table?
In this case the Status table clearly contains the primary key of the FK relationship here. If it was the other way around, then each status would have to exist in the Bug table before it could exist in the Status table, which clearly isn't intended.
是的,这是正确的。 思考这个问题的方法是,你的错误有一个状态; 任何给定状态都有许多错误。
Yes, this would be correct. The way to think of this would be that your bug has a status; there are many bugs with any given status.