Javascript sequelize 列的唯一约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28116187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:14:04  来源:igfitidea点击:

Unique constraint on sequelize column

javascriptnode.jssequelize.js

提问by Jeff Fairley

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.

使用 NodeJS 和 Sequelize 2.0,我正在编写一个迁移来创建一个新表。除了主键之外,我还想将要强制执行的第二列标记为唯一的。我在文档中找不到任何关于此的信息。

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

回答by Yuri Zarubin

The following works:

以下工作:

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