69

Using NodeJS and Sequelize 2.0, I'm writing a migration to create a new table. In addition to the primary key, I want to mark a second column to be enforced as unique. I can't find anything about this in the documentation.

migration.createTable('data', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    key: {
        // needs to be unique
        type: DataTypes.UUID,
        allowNull: false
    }
})
    .then(function () {
        done();
    });

1 Answer 1

139

The following works:

key: {
    // needs to be unique
    type: DataTypes.UUID,
    allowNull: false,
    unique: true
}
12
  • 7
    Ok. Now I feel like an idiot for not just trying that. Thanks. :) Jan 23, 2015 at 18:29
  • @Yuri do you know where there are docs on Sequelize constraints?
    – Don P
    Aug 29, 2015 at 21:46
  • 10
    Following up two years later - how would you find this out from the docs, or without asking on SO?
    – Don P
    Mar 10, 2017 at 0:42
  • 1
    @DonP The documentation for Sequelize is pretty bad. As far as I can tell, there's no reference that shows every column option. The return values of most methods are documented only as "Promise." I'm going to cut a lot of slack for anyone whose needs are not addressed by that mess.
    – Tom
    Nov 20, 2018 at 16:40
  • 4
    unique is not listed in sequelizejs' doc for attribute validation. Don't understand why such a commonly used feature is not even mentioned in the doc.
    – user938363
    Apr 11, 2019 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.