MongoEngine / MongoEngine/mongoengine
Error when using unique_with and inheritance
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
When upgrading to mongo 2.6 I get this error (which I don't get under mongo 2.4).
Trying to save an instance of my document (SubclassDoc):
File "/Users/amos/Work/rfb-dev/eggs/mongoengine-0.8.7-py2.7.egg/mongoengine/document.py", line 294, in save
raise OperationError(message % unicode(err))
OperationError: Could not save document (command SON([('createIndexes', u'feed'), ('indexes', [{'name': u'content_1_user_1', 'key': SON([('content', 1), ('user', 1)]), 'unique': True, 'background': False, 'sparse': True, 'dropDups': False}])]) failed: Index with name: content_1_user_1 already exists with different options)
I think the problem is that I have a base document class and a class that derives it that want to create different unique indexes on the same collection.
class FeedItem(Document):
user = ReferenceField('User', required=True)
content = ReferenceField('Content', required=True, unique_with='user')
class SubclassDoc(BaseDoc):
pass
>>> BaseDoc._unique_with_indexes()
[{'fields': [('content', 1), ('user', 1)], 'unique': True, 'sparse': False}]
>>> SubclassDoc._unique_with_indexes()
[{'fields': [('content', 1), ('user', 1)], 'unique': True, 'sparse': True}]
I don't understand why this works on mongo 2.4 but not on mongo 2.6.
Thanks for any suggestions!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the FeedItem/SubclassDoc example against MongoDB 2.6 and compare it with 2.4. Inspect mongoengine/document.py around save line 294 and the _unique_with_indexes results for the base and subclass; done means inherited documents no longer fail because the same index name has different options.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100