I have a schema with a parent-child relationship between two objects. This is one to many, therefore, of course, it will be implemented as:
table Parents id table Children id parent_id
However, I also need to consider one of the children as a special case (let him call him an "advanced" child). I can come up with several ways to do this, including:
Just put the attribute on Children :
table Parents id table Children id parent_id is_promoted
which is clearly wrong because the database cannot guarantee consistency (at least not with foreign key constraints)
Create a foreign key in the Parents table:
table Parents id promoted_child_id table Children id parent_id
What was my first slope, but it has an obvious lack of cyclic dependence, so itβs probably not optimal.
Another option I can think of is to put a second foreign key in the Children table:
table Parents id table Children id parent_id promoted_parent_id
Allows me to place a unique index, thereby ensuring database consistency. One problem is that one might have a meaningless relationship, where the child of the parent A lists as the advanced parent B.
The last option that I can think of is to create an intermediate table, for example:
table Parents id table Children id table ParentChildRelationship parent_id child_id is_promoted
Again, I can declare a unique restriction on parent_id + is_promoted to ensure consistency. I am a bit ambivalent in this matter, since it seems that it is unnecessary to encourage the attitude towards the complete entity, although the moment I add attributes to it (which, in essence, is_promoted ), I assume that it is justified.
I would like to know what you think is the canonical way to solve this problem. In particular, I use Rails, so this may affect the most practical solution.