I need to organize the storage of records in the database with the ability to mark them as favorites. The record looks like this.

{ "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" } 

Here are the ways to implement it:

enter image description here

enter image description here

What better way to do?

  • one
    Your task is not completely clear ... Chosen by whom and for whom? In other words - one record can be elected (or have a favorite) no more than once? Those. In your example, can a user have at most one favorite? If so, then I would choose the second option, otherwise the first one with ( one-to-many or zero-to-many ) connection - MaxU
  • Favorite user, in the application pokes a star, can be selected only once. - Kota1921 2:19 pm
  • one
    Does the system require more than one user? ;) - MaxU

1 answer 1

If the system assumes more than one user :) Then of course a separate table.

Minimum set of fields:

 message_id - ид сообщения, или что у вас в системе помечается? user_id - ид пользователя 

Those. typical many-to-many relationship. Each message can be flagged by multiple users; one user can mark multiple messages.

  • No, only one user. So the second option. - Kota1921 pm
  • @nekaneka, :). - pegoopik 4:47 pm