Unique constraint on sequelize column

Javascriptnode.jssequelize.js

Javascript Problem Overview


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();
    });

Javascript Solutions


Solution 1 - Javascript

The following works:

key: {
    // needs to be unique
    type: DataTypes.UUID,
    allowNull: false,
    unique: true
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeff FairleyView Question on Stackoverflow
Solution 1 - JavascriptYuri ZarubinView Answer on Stackoverflow