Imagine I have a child table called offers. offers can be referred from multiple tables such as purchase, membership, etc. Notice that in future, there can be more tables it will be referenced from.
Purchases and memberships are already present in the database and they will always be created before the corresponding offer object. Think of it like a purchase X may provide an offer Y (one to one)
In order to implement this in a relational schema, I thought of two possible options.
Option 1
offers table will not have any references. instead, purchase table and membership table will have a column named offer_id which references the offer table. As given below
table offers {
id,
offer_name,
}
table purchase {
id,
..other fields,
offer_id fk(offers),
}
table membership {
id,
..other fields,
offer_id fk(offers),
}
Option 2
offers table will contain a type field which refers the type of offer (purchase, membership, etc..) and there will be multiple nullable foreign keys referring to each table.
table offers {
id,
offer_name,
type enum(purchase, membership,
purchase_id fk(purchases) NULL,
membership_id fk(memberships) NULL,
}
table purchase {
id,
..other fields,
}
table membership {
id,
..other fields,
}
I felt that Option 1 is the simplest way to do it and also the right way. But it feels like going against database design principles (Parent should not refer child, etc..).
What should be the right way to do this? If I go with option 1, what will go wrong?