Skip to content

Commit

Permalink
docs(migrations): add example for conditional unique index (#12578)
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Almeida--Lemaire <valentin.almeida@onrewind.com>
Co-authored-by: Pedro Augusto de Paula Barbosa <papb1996@gmail.com>
  • Loading branch information
3 people committed Aug 1, 2020
1 parent 45ec1a2 commit 62f1911
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion docs/manual/other-topics/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,34 @@ module.exports = {
};
```

The next example is of a migration that creates an unique index composed of multiple fields with a condition, which allows a relation to exist multiple times but only one can satisfy the condition:

```js
module.exports = {
up: (queryInterface, Sequelize) => {
queryInterface.createTable('Person', {
name: Sequelize.DataTypes.STRING,
bool: {
type: Sequelize.DataTypes.BOOLEAN,
defaultValue: false
}
}).then((queryInterface, Sequelize) => {
queryInterface.addIndex(
'Person',
['name', 'bool'],
{
indicesType: 'UNIQUE',
where: { bool : 'true' },
}
);
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Person');
}
}
```

### The `.sequelizerc` file

This is a special configuration file. It lets you specify the following options that you would usually pass as arguments to CLI:
Expand Down Expand Up @@ -534,4 +562,4 @@ npx sequelize-cli db:migrate --url 'mysql://root:password@mysql_host.com/databas

### Programmatic usage

Sequelize has a sister library called [umzug](https://github.com/sequelize/umzug) for programmatically handling execution and logging of migration tasks.
Sequelize has a sister library called [umzug](https://github.com/sequelize/umzug) for programmatically handling execution and logging of migration tasks.

0 comments on commit 62f1911

Please sign in to comment.