3

I have seen some similar questions related to this but have not found an answer.

I am attempting to create a Gallery in my Keystone Project that is similar to a post, where there will be a list of galleries and in it a gallery with a set of selected images:

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();

I am able to create one gallery successfully - but any subsequent galleries throw the error:

There was an error saving your changes: insertDocument :: caused by :: 11000 E11000 duplicate key error index: site-name.galleries.$key_1 dup key: { : null } (MongoError)

I am at a loss for what I need to change to this model to allow unique slugs for my galleries to be directly linked to etc.

Codermonk
  • 883
  • 16
  • 32

2 Answers2

6

Completely delete the model from MongoDB and restart. I typically see this error when making changes to a model with indexes that have been defined previously

0

with insert new item,you should set an default value for 'name', because name is set unique=true. in my case, key is set to unique.

before:

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

after:

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