【问题标题】:How to sort data Sequelize belongsToMany associations?如何排序数据 Sequelize belongsToMany 关联?
【发布时间】:2019-03-20 14:52:59
【问题描述】:

我正在尝试使用 Sequelize 对以下架构的结果进行排序。但我收到类似Unable to find a valid association for the model, 'UserProject'的错误

请在下面找到我的代码,实际上,我想按UserProject.role对结果进行排序

UserProject = sequelize.define('user_project', { role: Sequelize.STRING});
User.belongsToMany(Project, { through: UserProject });
User.findAll({
    include: {
      required: true,
      through: {
        attributes: [],
        model: UserProject
      },
      model:Project,
      order: [[{ model: UserProject}, "role", "ASC"]]
});

提前致谢!!

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    试试这个怎么样?

    User.findAll({
        include: {
          required: true,
          through: {
            attributes: [],
            model: UserProject
          },
          model:Project
        },
        order: [[UserProject, "role", "ASC"]]
    });
    

    【讨论】:

    • 我遇到同样的错误
    猜你喜欢
    • 2017-08-15
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多