查了半天API文档,仔细看才发现这么一条
[attributes.column.unique=false] | String | Boolean | If true, the column will get a unique constraint. If a string is provided, the column will be part of a composite unique index. If multiple columns have the same string, they will be part of the same unique index |
假如为bool型的true,则表示单个这个列建唯一索引
假如为字符串,别的列中使用相同字符串的跟这个组成联合唯一索引,所以就实现了
"use strict";
module.exports = function (sequelize, DataTypes) {
var Store = sequelize.define("Store", {
storeId:{type:DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
name: {type:DataTypes.STRING, unique:"uk_t_store"},
address: DataTypes.STRING,
status: {type: DataTypes.INTEGER, defaultValue: 1},
areaId: {type:DataTypes.UUID,allowNull: false, unique:"uk_t_store"}
}, {
tableName: 't_store'
});
return Store;
};
本例中,areaId同时又是外键,关联一张area表,可以这么写
db['Store'].belongsTo(db['Area'], {foreignKey: "areaId", constraints: false});
同时附上另一张表的表结构
"use strict";
module.exports = function (sequelize, DataTypes) {
var Area = sequelize.define("Area", {
//区域ID
areaId: {type: DataTypes.UUID, primaryKey: true, allowNull: false, defaultValue: DataTypes.UUIDV4},
//区域名
name: {type:DataTypes.STRING, unique:"uk_t_area"},
//区域状态 1-启用 0-禁用
status: {type: DataTypes.INTEGER, defaultValue: 1}
}, {
tableName: 't_area'
});
return Area;
};