Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define partial index in Sequelize migration?

I'm using the following index in my model definition right now:

{
  name: 'unique_partner_id',
  unique: true,
  fields: ['partnerId'],
  where: {
    email: {
      $ne: null
    }
  }
}

However, I want to use migrations instead of sync, so I'm trying to move this definition from model to the initial migration file.

There is a queryInterface.addIndex() method, however, I can't find any documentation for it.

SO, how do I define a partial index using queryInterface?

like image 614
Slava Fomin II Avatar asked Feb 16 '16 20:02

Slava Fomin II


1 Answers

I was looking for how to do this in a model and your question answered that for me. So now I'm going to answer this for you! Here's the up function that worked for me:

up: function(queryInterface, Sequelize) {
  return queryInterface.addIndex(
    'tablename',
    ['column1', 'column2'],
    {
      name: 'indexname',
      where: {column3: {[Sequelize.Op.ne]: null}}
    }
  );
}
like image 191
PRS Avatar answered Sep 28 '22 07:09

PRS