Sequelize.js sequelize-按列从包含的模型订购

Sequelize.js sequelize-按列从包含的模型订购,sequelize.js,sql-order-by,Sequelize.js,Sql Order By,我不知道如何按包含模型中的列排序。但我甚至没有在属性中返回该字段。如果可能的话,我只想订购它。或者我可以返回它,只要我能按它排序就不用了 const users = await db.User.findAll({ raw: true, attributes: [ 'id', 'first_name', 'last_name', 'email', 'Permission.type' ],

我不知道如何按包含模型中的列排序。但我甚至没有在属性中返回该字段。如果可能的话,我只想订购它。或者我可以返回它,只要我能按它排序就不用了

const users = await db.User.findAll({
    raw: true,
    attributes: [
        'id',
        'first_name',
        'last_name',
        'email',
        'Permission.type'
    ],
    include: [
    {
        model: db.Permission,
        where: { id: { [Op.lte]: 2 } },
        attributes: [],
        order: [[{ model: db.Permission }, 'id', 'DESC']]
    }
    ]
});

include
块中,
order
块现在不执行任何操作。但是,我的权限模型中有一个名为
id
的属性,我想根据该属性对所有结果进行排序。如果有必要,我可以在我的
属性中添加
权限.id
。但是我试过了,但还是不起作用。

只需将
顺序
选项从
包含
移动到主选项:

const users = await db.User.findAll({
    raw: true,
    attributes: [
        'id',
        'first_name',
        'last_name',
        'email',
        'Permission.type'
    ],
    include: [
    {
        model: db.Permission,
        where: { id: { [Op.lte]: 2 } },
        attributes: []
    }
    ],
    order: [[{ model: db.Permission }, 'id', 'DESC']]
});

您也可以在
include
属性中指示
id

只需将
顺序
选项从
include
移动到主选项:

const users = await db.User.findAll({
    raw: true,
    attributes: [
        'id',
        'first_name',
        'last_name',
        'email',
        'Permission.type'
    ],
    include: [
    {
        model: db.Permission,
        where: { id: { [Op.lte]: 2 } },
        attributes: []
    }
    ],
    order: [[{ model: db.Permission }, 'id', 'DESC']]
});

另外,也许您应该在
include
attributes中指出
id

好的,这很有效!我以前无法让它工作,但我同时改变了好几件事。我还必须在主属性中返回id。如果我尝试在include属性中返回它,它会断开,但我没有得到任何结果。请不要尝试同时更改多个内容。这样你就说不出到底是什么导致了这个问题。好吧,那就行了!我以前无法让它工作,但我同时改变了好几件事。我还必须在主属性中返回id。如果我尝试在include属性中返回它,它会断开,但我没有得到任何结果。请不要尝试同时更改多个内容。这样,您就无法确切说出问题的起因。