NodeJS Sequelize使主表降序示例

时间:2017-02-06 05:22:58

标签: node.js postgresql sequelize.js

我遇到了如何在NodeJS中制作降序主表的问题。我发展得像那样

models.Users.findAll({
    where: {id: usersArray},
    include: [
        {model: models.Users_Answers}
    ],
    order: [
        [models.Users_Answers, 'id', 'ASC']
    ]
}).then(function(answers) {
    /* Do something */ 
});

但是当我改变如下时遇到错误,遇到错误。

models.Users.findAll({
    where: {id: usersArray},
    include: [
        {model: models.Users_Answers}
    ],
    order: [
        [models.Users, 'id', 'DESC']
        [models.Users_Answers, 'id', 'ASC']
    ]
}).then(function(answers) {
    /* Do something */ 
});
  

未处理拒绝错误:订单/组子句中的'用户'不是   有效的关联

1 个答案:

答案 0 :(得分:2)

你需要这样做,

models.Users.findAll({
    where: {id: usersArray},
    include: [
        {model: models.Users_Answers}
    ],
    order: [
        [db.Sequelize.col('id'), 'DESC'],
        [models.Users_Answers, 'id', 'ASC']
    ]
}).then(function(answers) {
    /* Do something */ 
});