Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: effects of "unique" property in model definition

I'm using Sequelize v3.5.1 with PostgreSQL v9.4.4 on a NodeJS server project.

In the model definition, it's not entirely clear to me what are the effects of adding the option unique: true to a property.

Let's take this code for example:

sequelize.define('User', {
  email: {
    type: Sequelize.STRING,
    unique: true
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false
  }
});

Does this mean that PostgreSQL will build a unique index on email? So, is it just a shorthand method for this?

sequelize.define('User', {
  email: {
    type: Sequelize.STRING,
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false
  }
}, {
  indexes: [
    {
      unique: true,
      fields: ['email']
    }
  ]
});

If so, will such index speed up table queries for email, or just ensure uniqueness?

Thanks!

like image 826
Pensierinmusica Avatar asked Nov 20 '25 21:11

Pensierinmusica


1 Answers

A unique index will do both, it will speed up queries (as it has to create an index), and it will ensure that every value is unique.

like image 84
Yuri Zarubin Avatar answered Nov 23 '25 21:11

Yuri Zarubin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!