【问题标题】:Strapi update many-to-many relationships in strapi?Strapi更新strapi中的多对多关系?
【发布时间】:2020-12-27 14:03:00
【问题描述】:

我在 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 查询的最佳方法是什么?

【问题讨论】:

  • 我最终编写了自定义 SQL 查询。
  • 这里也一样,写了自定义查询来修改关系。似乎是我猜的唯一方法。

标签: strapi bookshelf.js


【解决方案1】:

对于多对多关系更新,目前似乎没有任何直接的支持。因此,要么在上述 PUT api 中编写自定义查询,要么直接创建单独的 api 来更新由strapi 创建的** user_saved_stores__stores_saved_users** 表

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 2022-12-15
    • 1970-01-01
    • 2020-07-08
    • 2019-11-26
    • 2021-12-06
    • 2017-01-30
    相关资源
    最近更新 更多