How to define an index, within a Sequelize model? How to define an index, within a Sequelize model? sql sql

How to define an index, within a Sequelize model?


Almost there. You should add indexes in a new object like this:

module.exports = (sequelize, DataTypes) => {    const Item = sequelize.define('Item', {        itemId:  DataTypes.STRING,        ownerId:  DataTypes.INTEGER,        status: DataTypes.STRING,        type: DataTypes.STRING,        nature: DataTypes.STRING,        content: DataTypes.STRING,        moment: DataTypes.BIGINT    },    {      indexes:[       {         unique: false,         fields:['ownerId']       }      ]    });    return Item;};


It can work in single migration also.

In my case, just perform the addIndex after createTable method in the migration file

Migration:

return queryInterface.createTable('Item', {    // columns...}).then(() => queryInterface.addIndex('Item', ['OwnerId'])).then(() => {    // perform further operations if needed});

it's work for me in the migration file.