I have 2 User tables and Role . And since the relationship between them is much to many there is a third table User_role .

When updating the user, how to write the query correctly so that from the beginning in the user_role table all the records for the given user were deleted, and then new ones were recorded (which arrive when the user finds)?

Now I’ll have a user update for now, without affecting the third table:

 "UPDATE \"user\" SET email = ?, first_name = ?, second_name = ?, last_name = ?, password = ? WHERE id = ?"; 
  • Be sure to update all data at once? Maybe you should update the "personal data" separately, and the roles separately? - Streletz
  • Maybe you need to write 3 requests? The first update is the user table, the second is removed from the link table, all the records associated with this user. and the third rewrites the roles in the link table. - Aleksei
  • It's just that if only “personal data” changes, and the roles don't change, why touch them? When updating roles only, what's the point of updating other information? Again, what's the point when updating roles to erase everything and write again, when you can simply add new roles and / or delete those that the user should no longer have? - Streletz
  • both personal data and roles change. The meaning of erasing all the roles when updating them is that it’s easier for me =) - Aleksei
  • Write as many requests as you need. But often such requests must be performed in a single transaction. - Sergey

0