In the error / error model, where will the foreign key reside?

I always get stuck on foreign keys, so I have a question about where the foreign key should be in the bug tracking system where the bug has one status at any given time as long as only a small number of statuses exist (Open, Investigation, solutions pending approval). Thus, each status has many errors associated with it. My guess is that the foreign key should be in the Bug table as the 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

      

0


a source to share


3 answers


Are you correct in your assumption, more importantly, then the relationship (One-Many / One-One / Many-Many) determines which table is the primary key table and which is the foreign key table?



In this case, the state table explicitly contains the primary key of the relationship FK. If it were the other way around, then every status must exist in the Bug table before it can exist in the status table, which is clearly not intended.

+1


a source


Yes, you are correct in this assumption. As long as each Bug will only have one status, you can simply include the foreign key in this table.



+3


a source


Yes, that would be correct. The way to think about this would be that your error has a status; there are many bugs with any status.

0


a source







All Articles