# Is my ERD correct?

**URL:** <https://www.phphelp.com/t/is-my-erd-correct/33433>\
**Category:** MySQL database\
**Created:** [May 1, 2021, 10:05am UTC](https://www.phphelp.com/t/is-my-erd-correct/33433 "2021-05-01T10:05:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![idontknowphp](https://www.phphelp.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@idontknowphp](https://www.phphelp.com/u/idontknowphp)\
**Post date:** [May 1, 2021, 10:05am UTC](https://www.phphelp.com/t/is-my-erd-correct/33433/1 "2021-05-01T10:05:16Z")

</div>

![image](https://www.phphelp.com/uploads/default/original/2X/5/5fe675859184ee74d7efe27f97329ee4c15e49d2.png)

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [May 1, 2021, 10:51am UTC](https://www.phphelp.com/t/is-my-erd-correct/33433/2 "2021-05-01T10:51:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![idontknowphp](https://www.phphelp.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@idontknowphp](https://www.phphelp.com/u/idontknowphp)\
**Post date:** [May 1, 2021, 11:50am UTC](https://www.phphelp.com/t/is-my-erd-correct/33433/3 "2021-05-01T11:50:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [May 1, 2021, 12:26pm UTC](https://www.phphelp.com/t/is-my-erd-correct/33433/4 "2021-05-01T12:26:25Z")

</div>

Why did you choose a many to many relationship?

---

<div class="post-metadata">

**Author:** ![idontknowphp](https://www.phphelp.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@idontknowphp](https://www.phphelp.com/u/idontknowphp)\
**Post date:** [May 2, 2021, 2:37am UTC](https://www.phphelp.com/t/is-my-erd-correct/33433/5 "2021-05-02T02:37:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [May 2, 2021, 6:59pm UTC](https://www.phphelp.com/t/is-my-erd-correct/33433/6 "2021-05-02T18:59:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![idontknowphp](https://www.phphelp.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@idontknowphp](https://www.phphelp.com/u/idontknowphp)\
**Post date:** [May 3, 2021, 12:50am UTC](https://www.phphelp.com/t/is-my-erd-correct/33433/7 "2021-05-03T00:50:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BFlokstra](https://www.phphelp.com/letter_avatar_proxy/v4/letter/b/6de8d8/32.png) [@BFlokstra](https://www.phphelp.com/u/BFlokstra)\
**Post date:** [May 3, 2021, 2:05pm UTC](https://www.phphelp.com/t/is-my-erd-correct/33433/8 "2021-05-03T14:05:37Z")

</div>

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.
