E
E
elailasou2015-12-03 17:05:09
MySQL
elailasou, 2015-12-03 17:05:09

Simultaneous one-to-many and many-to-many relationship?

There is a one-to-many relationship Users->Posts. That is, one user can have many posts, and one post can have one user.
The task is that each user would have saved posts. Create a join table for a many-to-many relationship? Something like saved_users_posts? What kind of connection will this be?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nelson, 2015-12-03
@nelson

Are you talking about the implementation of the "Favorite"?
A table is made with columns user_id, post_id and the same unique primary index.
If for a post you need to display a list of users who saved it, add another index separately by post_id.

D
Dmitry Kovalsky, 2015-12-03
@dmitryKovalskiy

userId PK
savedMessageId PK
Composite key for two identifiers. It seems like nothing particularly complicated.

S
spot62, 2015-12-03
@spot62

there should be three tables:
1) user_id table
2) post_id->user_id post table
3) subscription table user_id<->post_id
=======
1 | User1
2 | User2
3 | User3
=======
=======
1 | Post 1 | 1 // owner User 1
2 | Post 2 | 1 // owner User 1
3 | Post 3 | 2 // owner User 2
4 | Post 4 | 1 // owner User 1
=======
=======
.. | 2 | 1 // User 2 subscribed to Post 1
.. | 3 | 1 // User 3 subscribed to Post 1
.. | 3 | 2 // User 3 subscribed to Post 2
.. | 2 | 2 // User 2 subscribed to Post 2
.. | 2 | 4 // User 2 subscribed to Post 4
=======

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question