使用 Postgresql 时如何在 SequelizeJS 模型中添加不区分大小写的唯一约束?
How to add a case-insensitive unique constraint in a SequelizeJS Model when using Postgresql?
var Model = sequelize.define('Company', {
name: {
type: DataTypes.STRING,
unique: true
}
}
在上面的例子中,unique:true是区分大小写的。它允许 "sample" 和 "Sample" 都保存在数据库中。在 sequelize 中是否有一种内置的方法可以做到这一点而无需编写自定义验证器?
我建议添加一个功能性唯一索引,如下所示:http://www.postgresql.org/message-id/c57a8ecec259afdc4f4caafc5d0e92eb@mitre.org
var Model = sequelize.define('Company', {
name: {
type: DataTypes.STRING
}
},
{
indexes: [
{
unique: true,
name: 'unique_name',
fields: [sequelize.fn('lower', sequelize.col('name'))]
}
]
});
var Model = sequelize.define('Company', {
name: {
type: DataTypes.STRING,
unique: true
}
}
在上面的例子中,unique:true是区分大小写的。它允许 "sample" 和 "Sample" 都保存在数据库中。在 sequelize 中是否有一种内置的方法可以做到这一点而无需编写自定义验证器?
我建议添加一个功能性唯一索引,如下所示:http://www.postgresql.org/message-id/c57a8ecec259afdc4f4caafc5d0e92eb@mitre.org
var Model = sequelize.define('Company', {
name: {
type: DataTypes.STRING
}
},
{
indexes: [
{
unique: true,
name: 'unique_name',
fields: [sequelize.fn('lower', sequelize.col('name'))]
}
]
});