Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SailsJS and Mongo unique attribute is ignored

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 0.10.x with [email protected]

Model:

module.exports = {

  attributes: {
    username:{
        type:'string',
        unique:true,
        required:true
    }
  }
};

Connections.js config file

mongo: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
    schema:true,
    migrate: 'safe',
    database: 'mydatabase'
},
like image 842
Jacob Bolton Avatar asked Aug 19 '14 11:08

Jacob Bolton


1 Answers

Answered on GitHub here: https://github.com/balderdashy/sails-mongo/issues/181

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".

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.

like image 93
particlebanana Avatar answered Oct 05 '22 23:10

particlebanana