Duplicate Key Error using autokey

516 views
Skip to first unread message

Sean Patterson

unread,
Dec 8, 2014, 2:42:50 PM12/8/14
to keyst...@googlegroups.com
Hi All,

I am trying to figure out why I always get an error when trying to create a second gallery in my keystone project. I can always delete the first and make a new one, but I can never make a second.

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)

In the code below I am using the autokey parameter, but it does not seem to have an affect, I switched the path to key and the error above then swtiched to saying $slug_1 dup slug {: null} - so that just confused me more.

What am I missing and do I need to reset my DB? If so how would I even do that, I am very unfamiliar with Mongo DB

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


atlb...@gmail.com

unread,
Dec 8, 2014, 2:53:55 PM12/8/14
to keyst...@googlegroups.com
You would typically see this error when you have an established index in MondoDB that conflicts with the list you are registering.

You should try deleting any previous collection from MongoDB to remove the indexes.

Sean Patterson

unread,
Dec 8, 2014, 3:31:04 PM12/8/14
to keyst...@googlegroups.com
My question is then if I delete all the galleries from the Admin UI shouldn't that remove all indexes from the mongodb? I think this is the case as I am able to make a new record for galleries when there are none, this error only shows up when attempting to make another.

atlb...@gmail.com

unread,
Dec 8, 2014, 3:44:44 PM12/8/14
to keyst...@googlegroups.com
I am not sure exactly what you mean when you say delete the galleries from the Admin UI.   If you make changes to a model that includes changing an indexed field you need to remove that collection from MongoDB using the CLI or a 3rd party app (robomongo) that interfaces directly with MongoDB and restart your Keystone app which will create a new collection instead of using a previously defined one.

When you register more than one gallery are you assigning a new list name?
new keystone.List('Gallery');
new keystone.List('AnotherGallery');


Sean Patterson

unread,
Dec 8, 2014, 4:01:45 PM12/8/14
to keyst...@googlegroups.com
Sorry I was unclear, but your suggestion to drop the collection from the mongodb worked perfectly, thank you for the robomongo suggestion, worked like a charm!

atlb...@gmail.com

unread,
Dec 8, 2014, 9:00:21 PM12/8/14
to keyst...@googlegroups.com
Anytime!  Since you are working on new collections the delete method is simple and quick.  If you make changes to a collection that you do not want to delete their are methods that you can use from the CLI or from the robomongo command line.




Since the Keystone List functions work off of Mongoose and  implement additional functionality, you should get familiar with the MongoDB docs and Mongoose Docs


Reply all
Reply to author
Forward
0 new messages