I have 4 basic models which are stored in following tables:
- users
- images
- videos
- posts
In application logic user may "love" image
or url
or post
(for example user clicks little heart next to image or video etc). So I have to keep all "loves" in database. My idea is to use table loves
with following columns:
id - love id, primary_key
user_id - id of user who loves something, foreign_key
entity_type - type of entity which is loved by user, enum('image', 'video', 'post')
entity_id - id of entity which is loves (either image_id, video_id, post_id)
unique index on (user_id,entity_type,entity_id) -> user can love unique entity only once
In this solution I can't declare foreign_key
on loves.entity_id
because I dont know which table it refers to...
Is this a good approach?
Is there better solution than this?
I am using postgres
but I tagged question with mysql
because maybe is general problem in relational dbs?