Strapi update many-to-many relationships in strapi?

Viewed 1829

I have two collection types in Strapi, created with many-to-many relation. What is the proper way to write a custom query to update only the relation?

**user**
id     name
1      user1
**store**
id     name
1      store1
**user_saved_stores__stores_saved_users**
id     user_id     store_id
1      1           1

I can use the default PUT API to update the relation like this, but I would need to find out every single user before calling the API, as it would overwrite all the many-to-many relations.

const xhr = new XMLHttpRequest();
xhr.open('PUT', '/stores/1', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(
  JSON.stringify({
    savedUser: ["1","2"],
  })
);

If I would like to add or remove only one user to the relations, what should be the best way without writing custom SQL query?

1 Answers

Currently seems not any straight forward support in strapi for many to many relationships update. So either writing custom queries inside the above PUT api , of create separate api directly to update the ** user_saved_stores__stores_saved_users** table created by strapi

Related