Mazzy
Mazzy

Reputation: 14179

Error in sequelize column is not found

I have created the following model in sequelize:

export function initialize(sequelize: Sequelize.Sequelize, dataTypes: Sequelize.DataTypes): SalesPerson.Model {
    var salesPersonModel = sequelize.define<SalesPerson, SalesPerson.Pojo>('salesPerson', {
        ownerId: { type: dataTypes.STRING, allowNull: false, field: 'owner_id' },
        firstName: { type: dataTypes.STRING, allowNull: false, field: 'first_name' },
        lastName: { type: dataTypes.STRING, allowNull: false, field: 'last_name' },
        email: { type: dataTypes.STRING, allowNull: false }
    }, {
        underscored: true,
        tableName: 'sales_persons'
    });

    salesPersonModel.removeAttribute('id');

    return salesPersonModel;
}

after starting sequelize.sync() the table is correctly created. the problem is that when I make a query like this:

db.SalesPerson.find({ limit: 1, order: [['updated_at DESC']] })...

I got the following error:

Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'salesPerson.updated_at DESC' in 'order clause'

and this has not sense because I see in the database the column so should be something related with sequelize

Upvotes: 0

Views: 1067

Answers (1)

Amit
Amit

Reputation: 46323

That's not how the syntax works. You need to separate the column name from the direction:

db.SalesPerson.find({ limit: 1, order: [['updated_at', 'DESC']] })...

Upvotes: 1

Related Questions