This looks like a forum.
I’m guessing:
- Users can create posts.
- Users can create comments on posts.
- Users can like posts.
- Users can like comments.
If this is all you’re doing, then you’ve overcomplicated a bit.
You don’t need posts_has_users
or comments_has_users
. Each post and comment has a single account associated with it - a one to one relationship - so you don’t need link tables. Just have a created_by_id
column on posts
and comments
that link to a user.
There are a few ways to do likes. I would have a post_likes
table that links a user to a post, and a comment_likes
table that links a user to a comment. This makes it more complicated to list everything a user has liked, but simpler to do anything else.
I didn’t create the comments_has_users and likes_has_users tables… they were automatically created as I have chosen many-to-many relationship
Why did you choose a many to many relationship?
because users can make many comments and comments can be made my many users…
users can make so many posts and posts can be made by many users
likes can be made by many users and users can like as many as they want
When figuring out relationships, you need to look at a single example of the entity you’re modelling. A single user can make many comments, but a single comment can only be made by one user. These are one to many relationships.
ok tnx for this, last night I was waiting for your answer but I think we have different timezones… I just made all the tables one to many relationships and I manually filled the data and there were no errors… so I guess my diagram is kinda correct now. lol
but I am still confused with the relationships… I guess I will just have to test the tables to check if they are right by putting data into it
A little background information might be suitable:
- One to one relationship: One user can only ever make one comment
- One to many: One user can make many comments
- Many to many: Many users can have many comments and those comments can be co-written by multiple users at the same time.
Your situation is a one to many relationship. Every individual user can have many comments, but every individual comment can only have one user.