sequelize migration unique code example

Example 1: sequelize generate migration

npx sequelize-cli migration:generate --name add-title-post-table

Example 2: sequelize migration set unique constraint

// adding constraint while table creation
queryInterface.createTable('Posts', {
    title: {
        type: Sequelize.STRING(200),
    }
}, {
    uniqueKeys: {
        Items_unique: {
            fields: ['title']
        }
    }
});

// add constraint only
queryInterface.addConstraint('Posts', ['title'], {
  type: 'unique',
  name: 'unique_constraint_name'
});

Tags:

Misc Example