Schema Setup - Likes

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 384:- Schema Setup - Likes

To set up the schema for likes, we need to consider the following:

  1. A like can belong to either a post or a comment.
  2. A like is associated with a user who created it.
  3. A user can like a post or a comment only once.
  4. The count of likes needs to be stored with the post or comment to avoid extra computation.

With the above considerations, we can set up the schema for likes as follows:

likes table:

  • id (primary key)
  • likeable_id (integer, foreign key)
  • likeable_type (string)
  • user_id (integer, foreign key)
  • created_at (timestamp)

Here, the likeable_id and likeable_type are used to define a polymorphic relationship with either the posts or comments table.

posts table:

  • id (primary key)
  • title (string)
  • content (text)
  • user_id (integer, foreign key)
  • likes_count (integer)

comments table:

  • id (primary key)
  • content (text)
  • post_id (integer, foreign key)
  • user_id (integer, foreign key)
  • likes_count (integer)

Here, we have added the likes_count column to the posts and comments tables to keep track of the total number of likes for a post or a comment.

With the above schema, we can create a new like by inserting a record into the likes table with the likeable_id, likeable_type, and user_id. We can then increment the likes_count for the corresponding post or comment. We can also check if a user has already liked a post or a comment by querying the likes table for a record with the given likeable_id, likeable_type, and user_id.

45. Friends + Likes

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support