Luis Crespo
Luis Crespo

Reputation: 1620

Sails 0.11 & Sails-Mongo 0.11.2 unique constraint not working

I'm using sails 0.11 and sails-mongo 0.11.2 adapter.

I have a user model with a nickname, and I want the nickname to be unique among every user. I specify the attribute as unique like this:

nickname: {
   type: 'string',
   unique: true
}

It is not working, I can add two users with the same nickname without a problem. Any clue on how to fix this issue?.

Upvotes: 2

Views: 324

Answers (3)

edencorbin
edencorbin

Reputation: 2949

Okay I was struggling with this exact issue and resolved it. In config/locales/models.js change migrate : 'safe' to migrate : 'alter'. Lift your application. If there are any duplicates in your unique field it will error on lift, and you will have to delete the duplicates. After cleaning up any duplicates lift. Unique should now work. You can now change back to migrate : 'safe' and unique should continue to be enforced.

Upvotes: 1

Luis Crespo
Luis Crespo

Reputation: 1620

By design, sails does no migrations in production, that's why mongo indexes are not generated.

One solution would be to add the indexes manually to the mongo collections were they are needed.

Another solution would be to migrate a local mongo DB from sails and replicate it into a production DB so it has the indexes.

Upvotes: 0

Yann Bertrand
Yann Bertrand

Reputation: 3114

Have you tried to specify the schema flag to true?

http://sailsjs.org/#!/documentation/concepts/ORM/model-settings.html

Upvotes: 0

Related Questions