问题描述
我在 Strapi 中有两种集合类型,它们是用多对多关系创建的。 编写自定义查询以仅更新关系的正确方法是什么?
**user**
id name
1 user1
**store**
id name
1 store1
**user_saved_stores__stores_saved_users**
id user_id store_id
1 1 1
我可以使用默认的 PUT API 来更新这样的关系,但我需要在调用 API 之前找出每个用户,因为它会覆盖所有的多对多关系。
const xhr = new XMLHttpRequest();
xhr.open('PUT','/stores/1',true);
xhr.setRequestHeader('Content-Type','application/json');
xhr.send(
JSON.stringify({
savedUser: ["1","2"],})
);
如果我只想在关系中添加或删除一个用户,不编写自定义 sql 查询的最佳方法是什么?
解决方法
对于多对多关系更新,目前似乎在 Strapi 中没有任何直接的支持。所以要么在上面的 PUT api 中编写自定义查询,要么直接创建单独的 api 来更新由 Strapi 创建的 ** user_saved_stores__stores_saved_users** 表