尝试在 Sequelize 中排序但出现错误 "Unable to find a valid association for model"

Trying to Order By in Sequelize but get error "Unable to find a valid association for model"

我正在尝试在 Sequelize 中订购包含关联(多对多)的 findByPk 结果,但我遇到了一些问题。我不断收到错误消息:

Error: Unable to find a valid association for model, 'Item'

查询:

        Guest.findByPk(id,
            {
                include: { model: db.Item, as: 'items' },
                order: [
                    [ { model: db.sequelize.models.Item, as: 'items' }, 'dexId', 'ASC' ]
                ]
            })
            .then(data => {
                res.status(200).json({ items: data.items});
            })
            .catch(err => {
                const error = new createError(500, "Error retrieving Guest with id=" + id);
                return next(error);
            });

物品型号:

'use strict';
const {
    Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
    class Item extends Model {
        /**
         * Helper method for defining associations.
         * This method is not a part of Sequelize lifecycle.
         * The `models/index` file will call this method automatically.
         */
        static associate(models) {
            Item.belongsToMany(models.Guest, {
                //through: 'GuestItems',
                through: {
                    model: 'GuestItems',
                    unique: false
                },
                constraints: false,
                as: 'guests',
                foreignKey: 'itemId',
                otherKey: 'guestId'
            });
            Item.belongsToMany(models.User, {
                //through: 'UserItems',
                through: {
                    model: 'UserItems',
                    unique: false
                },
                constraints: false,
                as: 'users',
                foreignKey: 'itemId',
                otherKey: 'userId'
            });
        }
    }

    Item.init({
        dexId: {
            allowNull: true,
            type: DataTypes.INTEGER,
            defaultValue: null
        },
        name: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false
        },
        description: DataTypes.STRING,
        filename: DataTypes.STRING,
    }, {
        sequelize,
        modelName: 'Item',
        paranoid: true
    });
    return Item;
};

客座模特

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Guest extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      Guest.belongsToMany(models.Item, {
        through: 'GuestItems',
        as: 'items',
        foreignKey: 'guestId',
        otherKey: 'itemId'
      });
    }
  }
  Guest.init({
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false
    },
    token: DataTypes.STRING,
    role: {
      type: DataTypes.ENUM,
      values: ["guest"],
      defaultValue: "guest"
    },
    lastIPAddress: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'Guest',
    associations: true
  });
  return Guest;
};

我知道 db.sequelize.models.Item 确实存在,我也试过调用 db.Item(同样存在并在别处使用)——但都没有用。 我也试过

  [ db.sequelize.models.Item, 'dexId', 'ASC' ]

而不是 { as 'items' } 位,但我仍然遇到该错误。

我使用的是最新版本的 Sequelize 和 Postgresql

Guest.findByPk(id, {
        include: {model: db.Item, as: 'items', required: true},
        order: [[sequelize.literal('"items"."dexId"'), 'ASC']] 
})
    .then((data) => {
            res.status(200).json({items: data.items});
    })
    .catch((err) => {
                return next(error);
    });