首页 > 解决方案 > 为什么续集包含顺序不起作用?

问题描述

我已经阅读了几个提到排序的问题,包括续集中的模型,但没有找到任何可以解决我的问题的问题。这个案子似乎很简单,所以我遗漏了一些东西,但不知道是什么......

任何帮助将不胜感激。

定义:

models.Account.belongsToMany(models.User, {through: 'AccountUser'});
models.User.belongsToMany(models.Account, {through: 'AccountUser'});

models.Account.hasMany(models.AccountUser)
models.AccountUser.belongsTo(models.Account)

models.User.hasMany(models.AccountUser)
models.AccountUser.belongsTo(models.User)

询问:

const user = await models.User.findOne({
        where: {
            code: userCode,
        },
        include: [
            {
                model: models.AccountUser,
                order: [ ['lastLoginAt', 'desc'] ],
                include: {
                    model: models.Account,
                }
            }
        ]
})

结果

AccountUser 数组不是按 DESC 而是按 ASC 排序的。

版本

续集:6.5.1 PostgreSQL:13.1

标签: postgresqlsequelize.js

解决方案


我完全错过了订单的位置(在根而不是包含)。我的错。

Node Sequelize 中预加载模型的排序结果


推荐阅读