【问题标题】:KeystoneJS - Create new Item throws duplicate key errorKeystoneJS - 创建新项目会引发重复键错误
【发布时间】:2015-02-01 18:36:31
【问题描述】:

我看到了一些与此相关的类似问题,但没有找到答案。

我正在尝试在我的 Keystone 项目中创建一个类似于帖子的画廊,其中将有一个画廊列表,其中包含一个包含一组选定图像的画廊:

var keystone = require('keystone'),
    Types = keystone.Field.Types;

/**
 * Gallery Model
 * =============
 */

var Gallery = new keystone.List('Gallery', {
    map: { name: 'name' },
    autokey: { path: 'slug', from: 'name', unique: true }
});

Gallery.add({
    name: { type: String, required: true},
    published: {type: Types.Select, options: 'yes, no', default: 'no', index: true},
    publishedDate: { type: Types.Date, index: true, dependsOn: { published: 'yes' } },
    description: { type: String },
    heroImage : { type: Types.Relationship, ref: 'Image' },
    images : { type: Types.Relationship, ref: 'Image', many: true }
});

Gallery.defaultColumns = 'title, published|20%, publishedDate|20%';
Gallery.register();

我能够成功创建一个画廊 - 但任何后续画廊都会抛出错误:

保存更改时出错:insertDocument :: 由 :: 引起 11000 E11000 重复键错误索引:site-name.galleries.$key_1 dup 键:{ : null } (MongoError)

我不知道我需要更改此模型以允许我的画廊的独特 slug 直接链接到等。

【问题讨论】:

    标签: javascript mongodb mongoose keystonejs


    【解决方案1】:

    从 MongoDB 中完全删除模型并重新启动。在使用先前定义的索引更改模型时,我通常会看到此错误

    【讨论】:

    • 这是正确的解决方案,只需要从 mongoDB 中删除整个集合
    【解决方案2】:

    在插入新项目时,您应该为“name”设置一个默认值,因为 name 设置为 unique=true。 在我的例子中,key 设置为唯一的。

    之前:

    MongoDB Enterprise > db.categories.save({_id: 89,name: 'xxx'})
    WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: sku.productcategories index: key_1 dup key: { : null }"
        }
    })
    

    之后:

    MongoDB Enterprise > db.categories.save({_id: 89,name: 'xxx', key:'xx'})
    WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 89 })
    

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 2019-12-16
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多