【问题标题】:Sails mongo unique: 'false' attribute not workingSails mongo 独特:“假”属性不起作用
【发布时间】:2018-04-25 23:21:36
【问题描述】:

我想使用sails js和mongodb多次更新值。但是,当我尝试使用以前存在的相同值更新值时,即使我设置了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 }"
    },

下面是使用get请求方法获取JSON数据:

[
    {
        "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"
    }
]

帆型:

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

【问题讨论】:

  • 以前,您的模型上有unique : true,然后您更改它?
  • 之前我没有定义唯一属性。
  • 问题出在数据库端而不是sails应用程序,看看数据库中是否有与order_index字段相关的唯一索引,我认为如果你旋转一个新的mongo实例,你应该是很好。
  • @HamzaFatmi,我发布了答案。

标签: mongodb sails.js sails-mongo


【解决方案1】:

我将order_index 类型从string 更改为array。这解决了问题。

【讨论】:

    猜你喜欢
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多