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

Viewed 280

I'm trying to order a findByPk result that includes an association (Many to many) in Sequelize, but I'm having some issues. I keep getting the error:

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

The query:

        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);
            });

Item model:

'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;
};

Guest model

'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;
};

I know for a fact db.sequelize.models.Item exists, I've also tried calling db.Item (Which again, exists and is used elsewhere) - and neither work. I've also tried

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

instead of the { as 'items' } bit, but I still get that error.

I'm using the latest versions of Sequelize and Postgresql

1 Answers
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);
    });
Related