Search code examples
databasedatabase-designdata-warehousedatabase-normalization

Database design: Likes table


I got two tables (for now):

  1. Topic
  2. Post (post is a comment for a topic)

I want to add the option to Like those objects.
so I thought about creating one table of Likes and using enum to indicate which object was liked (including the object's id of course).

by the way, if I choose this option, should it be an enum or another table represent all the objects:

id object_name
1 Topic
2 Post

another option is to create likes table for every object .

what is the best practice to take?


Solution

  • I think creating a separate table for each object is better.

    I don't see what you gain if you use only one table. You can't use foreign keys properly also in one table.

    I mean you can't add a column object_id to your table, because you do not know the table to which it will point to. In this case you have two add two columns, topic_id and post_id. Always one of the two will be NULL.