Unique property fails in Sails.js Unique property fails in Sails.js express express

Unique property fails in Sails.js


This is because your schema is not updated in your disk database (".tmp/disk.db").

You need to shutdown sails, drop your DB and restart sails.The DB will be reconstruct with your good schema.

Attention : the data will be drop too !

If you want keep your data, you can just update the schema part of ".tmp/disk.db".

What I have doing to keep data and rebuild schema by sails.js :

  1. copy ".tmp/disk.db"
  2. clean ".tmp/disk.db"
  3. shutdown sails.js
  4. start sails.js-> the database is empty and the schema is updated
  5. copy old "counters" part
  6. copy old "data" part

You must have this in your schema (file ".tmp/disk.db" -> "schema" part) for the unique field :

  "xxx": {    "type": "string",    "unique": true  },

I hope this help you.


I ran into this same issue. To solve it, you have to avoid using the 'disk' ORM adapter. For some reason it appears that it doesn't support uniqueness checks.

Other adapters such as mongo and mysql should support uniqueness checks, so this shouldn't be an issue outside of development.

For the course of development, change the default adapter in config/adapters.js from 'disk' to 'memory'. Should look like this:

module.exports.adapters = {  // If you leave the adapter config unspecified   // in a model definition, 'default' will be used.  'default': 'memory',  // In-memory adapter for DEVELOPMENT ONLY  memory: {    module: 'sails-memory'  },  ...};


I'm not certain this is the issue, but have you added schema:true to your models and adapters?

My mongo adapter config looks like this:

    module.exports.adapters = {            'default': 'mongo',            mongo: {                    module: 'sails-mongo',                    url: process.env.DB_URL,                    schema: true            }    };

And my User model looks like this (trimmed a bit):

    module.exports = {            schema: true,            attributes: {                    username: {                            type: 'string',                            required: true,                            unique: true                    }                    //...            }    };