且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sequelize中查询多对多关系

更新时间:2023-02-15 19:27:11

查询嵌套关联不是 Feathers 通用查询语法的一部分.在 Sequelize 的情况下,应根据您的需要使用 params.sequelize 并包括 feathers-sequelize 关联文档中所示::p>

Querying for nested associations is not part of Feathers common query syntax. In the case of Sequelize the query should be assembled according to your needs using params.sequelize and includes as shown in the feathers-sequelize associations documentation:

    // GET /my-service?name=John&include=1
    function (context) {
       if (context.params.query.include) {
          const AssociatedModel = context.app.services.fooservice.Model;
          context.params.sequelize = {
             include: [{
               model: AssociatedModel
               // normal Sequelize where query here
              }]
          };
          // delete any special query params so they are not used
          // in the WHERE clause in the db query.
          delete context.params.query.include;
       }

       return Promise.resolve(context);
    }