I have several entities, that can have multiple relationships.
For example, i have following entitites:
- entity_type
- tag
- tag_assignment
- news_post
- account
In entity_type table are described all entities that i have in my project (e.g. news posts, blog posts, messages, accounts, everything)
Entity_type table has just id and name fields, name field describes model class name for usability
Tag entity has just id and name. It’s standalone entity, that is mapped to other entities later with tag_assignment entity
Tag assignment entity has id, tag_id, entity_type_id and entity_id. Entity_type_id describes in which i can find entity, entity_id specifies entity in the table.
So i want to make following combined foreig key from one column to many tables:
- tag_assignment.entity_type_id => entity_type (id)
- tag_assignment.entity_id => news_post (id), account (id), etc
Is it possible to make this combined key? I mean if to make dependencies if i delete a row from entity_type table, everything will be dropped/updated in other tables, if i will delete account, only tag_assignments that have foreign key to account table will be deleted.
You should normalize your database by extracting a table called
entity. The concept of this table is similar to an abstract class in OOP. Thenews_post,accountand other entities you might have in your database should all reference theentitytable. This way you can reference any entity that you have now or might have in the future with a common, specific location.Also you might want to familiarize yourself with the EAV model. This might help you resolve similar design issues.