{"action":"created","issue":{"url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/930","labels_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/930/labels{/name}","comments_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/930/comments","events_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/930/events","html_url":"https://github.com/MongoEngine/mongoengine/issues/930","id":66452600,"number":930,"title":"Empty ListFields of EmbeddedDocuments with a unique field may only exist once (MongoEngine 0.9.0)","user":{"login":"eli-b","id":834999,"avatar_url":"https://avatars.githubusercontent.com/u/834999?v=3","gravatar_id":"","url":"https://api.github.com/users/eli-b","html_url":"https://github.com/eli-b","followers_url":"https://api.github.com/users/eli-b/followers","following_url":"https://api.github.com/users/eli-b/following{/other_user}","gists_url":"https://api.github.com/users/eli-b/gists{/gist_id}","starred_url":"https://api.github.com/users/eli-b/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eli-b/subscriptions","organizations_url":"https://api.github.com/users/eli-b/orgs","repos_url":"https://api.github.com/users/eli-b/repos","events_url":"https://api.github.com/users/eli-b/events{/privacy}","received_events_url":"https://api.github.com/users/eli-b/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/MongoEngine/mongoengine/labels/Discussion","name":"Discussion","color":"d4c5f9"},{"url":"https://api.github.com/repos/MongoEngine/mongoengine/labels/Documentation","name":"Documentation","color":"eb6420"},{"url":"https://api.github.com/repos/MongoEngine/mongoengine/labels/In%20Progress","name":"In Progress","color":"ededed"},{"url":"https://api.github.com/repos/MongoEngine/mongoengine/labels/ListField","name":"ListField","color":"fef2c0"}],"state":"closed","locked":false,"assignee":{"login":"seglberg","id":799727,"avatar_url":"https://avatars.githubusercontent.com/u/799727?v=3","gravatar_id":"","url":"https://api.github.com/users/seglberg","html_url":"https://github.com/seglberg","followers_url":"https://api.github.com/users/seglberg/followers","following_url":"https://api.github.com/users/seglberg/following{/other_user}","gists_url":"https://api.github.com/users/seglberg/gists{/gist_id}","starred_url":"https://api.github.com/users/seglberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seglberg/subscriptions","organizations_url":"https://api.github.com/users/seglberg/orgs","repos_url":"https://api.github.com/users/seglberg/repos","events_url":"https://api.github.com/users/seglberg/events{/privacy}","received_events_url":"https://api.github.com/users/seglberg/received_events","type":"User","site_admin":false},"milestone":null,"comments":21,"created_at":"2015-04-05T16:02:56Z","updated_at":"2015-05-12T13:05:03Z","closed_at":"2015-05-07T17:12:11Z","body":"Trying to save an empty list of embedded documents which have a unique field on another document causes the following error to be thrown:\r\n```\r\nE11000 duplicate key error index: mydb.mycollection.mylist.myuniquefield_1 dup key: { : null }\r\n```\r\n\r\nInterestingly, having a primary_key field in an EmbeddedDocument in an empty ListField doesn't cause an error.\r\n\r\nThe workaround we found is to define the field on the EmbeddedDocument as unique and sparse. which is funny, since the sparse option is the reason I upgraded to mongoengine 0.9.0 in the first place..."},"comment":{"url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/comments/101267949","html_url":"https://github.com/MongoEngine/mongoengine/issues/930#issuecomment-101267949","issue_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/930","id":101267949,"user":{"login":"seglberg","id":799727,"avatar_url":"https://avatars.githubusercontent.com/u/799727?v=3","gravatar_id":"","url":"https://api.github.com/users/seglberg","html_url":"https://github.com/seglberg","followers_url":"https://api.github.com/users/seglberg/followers","following_url":"https://api.github.com/users/seglberg/following{/other_user}","gists_url":"https://api.github.com/users/seglberg/gists{/gist_id}","starred_url":"https://api.github.com/users/seglberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seglberg/subscriptions","organizations_url":"https://api.github.com/users/seglberg/orgs","repos_url":"https://api.github.com/users/seglberg/repos","events_url":"https://api.github.com/users/seglberg/events{/privacy}","received_events_url":"https://api.github.com/users/seglberg/received_events","type":"User","site_admin":false},"created_at":"2015-05-12T13:05:03Z","updated_at":"2015-05-12T13:05:03Z","body":"Using the `primary_key` attribute on a Field essentially changes the fields db_name to \"_id\" (although this can be overridden) but also lets MongoEngine know to use that field as the `id` attribute. While closely related, it is not the same as having an index on the field. The index for the primary key is created by *MongoDB* and cannot be removed.\r\n\r\nI am assuming setting `primary_key` on an EmbeddedDocument's Field will force that field to be stored in the database as `_id` but no index will be created. And since there is no index being created, there is no uniqueness check."}}