在错误/错误状态模型中,外键将驻留在哪里?

发布于 2024-07-21 18:55:20 字数 403 浏览 9 评论 0原文

我总是被外键困住,所以我有一个问题,外键应该驻留在错误跟踪系统中的哪里,其中错误在任何给定时间都具有单一状态,而只存在少量状态(开放、正在调查、已解决,待批准)。 所以每个状态都有很多与之相关的错误。 我的假设是外键应该作为引用 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 技术交流群。

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

发布评论

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

评论(3

不即不离 2024-07-28 18:55:20

是的,你的假设是正确的。 只要每个错误只有一个状态,您就可以只包含该表的外键。

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.

日记撕了你也走了 2024-07-28 18:55:20

您的假设是正确的,更重要的是关系(一对多/一对一/多对多)决定哪个表是主键表,哪个是外键表?

在这种情况下,状态表显然包含这里 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.

硬不硬你别怂 2024-07-28 18:55:20

是的,这是正确的。 思考这个问题的方法是,你的错误有一个状态; 任何给定状态都有许多错误。

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.

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