Search code examples
mongodbsails.jssails-mongo

Sails mongo unique: 'false' attribute not working


I would like to update the value multiple times using sails js and mongodb. However, when I try to update the value with the same value that exist previously, its giving me an error even though I've set unique: 'false':

"code": "E_VALIDATION",
    "invalidAttributes": {
        "order_index": [
            {
                "rule": "unique",
                "value": 1,
                "message": "A record with that `order_index` already exists (`1`)."
            }
        ]
    },
    "originalError": {
        "name": "MongoError",
        "message": "E11000 duplicate key error collection: Brand_Compass.manufacturer_tabs index: order_index_1 dup key: { : 1 }",
        "driver": true,
        "index": 0,
        "code": 11000,
        "errmsg": "E11000 duplicate key error collection: Brand_Compass.manufacturer_tabs index: order_index_1 dup key: { : 1 }"
    },

Below is the JSON data fetch using get request method:

[
    {
        "order_index": 0,
        "tab_name": "tab 1",
        "createdAt": "2018-04-24T11:27:26.112Z",
        "updatedAt": "2018-04-24T11:27:26.225Z",
        "manufacturers": "5acf62cf080d700c2209d40b",
        "id": "5adf149e366e1a0e4085a4f1"
    },
    {
        "order_index": 1,
        "tab_name": "tab 2",
        "createdAt": "2018-04-24T11:27:31.043Z",
        "updatedAt": "2018-04-24T11:27:31.048Z",
        "manufacturers": "5acf62cf080d700c2209d40b",
        "id": "5adf14a3366e1a0e4085a4f2"
    }
]

Sails model:

module.exports = {
  attributes: {
    order_index: {
      type: 'integer',
      unique: 'false'
    },
    tab_name: {
      type: 'string'
    },
    manufacturer_fields: {
      model: 'manufacturer_fields'
    },
    manufacturers: {
      model: 'manufacturers'
    }
  }
};

Solution

  • I changed the order_index type from string to array. That fixed the issue.