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

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

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

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?