Search code examples
javascriptmongodbmongoosekeystonejs

KeystoneJS - Create new Item throws duplicate key error


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.


Solution

  • 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