SailsJS和Mongo唯一属性被忽略 [英] SailsJS and Mongo unique attribute is ignored
问题描述
在SailsJS中使用Mongo时,我似乎无法获得unique属性来实际检查/验证唯一值.我最终很容易得到相同的用户名.有什么想法吗?我检查了与此相关的另一篇文章,但这与使用磁盘/内存数据库有关.
I cant seem to get the unique attribute to actually check/validate for unique values while using Mongo in SailsJS. I end up with identical usernames easily. Any thoughts? I checked the other post about this, but that was related to using Disk/Memory databases.
带有sails-mongo@0.10.x的风帆0.10.x
Sails 0.10.x with sails-mongo@0.10.x
型号:
module.exports = {
attributes: {
username:{
type:'string',
unique:true,
required:true
}
}
};
Connections.js配置文件
Connections.js config file
mongo: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
schema:true,
migrate: 'safe',
database: 'mydatabase'
},
推荐答案
在GitHub上的此处回答: https://github.com/balderdashy/sails-mongo/issues/181
Answered on GitHub here: https://github.com/balderdashy/sails-mongo/issues/181
设置migrate: 'safe'
后,Waterline将不会尝试在数据库上创建任何索引.通过使用在迁移"期间在数据库上创建的唯一索引,可以实现唯一性.
When you have migrate: 'safe'
set Waterline will not attempt to create any indexes on the database. Unique works by using the unique indexes created on the database during a "migration".
如果您处于生产环境中,则应自己创建索引并保持migrate: 'safe'
的设置,以使Waterline不会触及您的生产数据.在开发中,您可以设置migrate: 'alter'
,并且应该创建这些索引.
If you are in a production environment, you should create the indexes yourself and keep migrate: 'safe'
set so that Waterline isn't touching your production data. In development you can set migrate: 'alter'
and these indexes should be created.
这篇关于SailsJS和Mongo唯一属性被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!