KeystoneJS - 创建新项目会引发重复键错误

时间:2014-12-04 01:08:25

标签: javascript mongodb mongoose keystonejs

我见过一些与此相关的类似问题但未找到答案。

我正在尝试在我的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   key:{:null}(MongoError)

我无法改变我需要改变的模型,以允许我的画廊中的独特slu to直接链接到等等。

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

使用insert new item,你应该为'name'设置一个默认值,因为name设置为unique = true。 在我的情况下,密钥设置为唯一。

之前:

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 })