MongoEngine / MongoEngine/mongoengine

Issue with embedded document uniqueness

Open
#1,373 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

HI there,

I am upgrading mongoengine from 0.8.6 to 0.10.6 and I am hitting issue when saving one of my models.

DuplicateKeyError: E11000 duplicate key error index: mbc_web.level_one.$list_level_twos.en_name_1 dup key: { : "Ambulance/Medical Travel" } 

As you can see from my models, I have define 3 unique fields on the LevelTwo model (l2_id, en_name, fr_name fields) as I would like LevelTwo model to be unique within the LevelOne.list_level_twos array. but not unique in the whole document. I would like to be able to have 2 LevelOne models that could have list_level_twos array that contains the same LevelTwo model

This used to work with 0.8.6, is there a option I need to include get the same functionality has before

Models:


class LevelTwo(db.EmbeddedDocument):
    l2_id = db.ObjectIdField(required=True, unique=True)
    en_name = db.StringField(max_length=80, required=True, unique=True)
    fr_name = db.StringField(max_length=80, required=True, unique=True)
    list_level_threes = db.ListField(db.EmbeddedDocumentField(LevelThree), default=[])

    def __repr__(self):
        return '<Provider: %r>' % self.en_name


class LevelOne(db.Document):
    en_name = db.StringField(max_length=80, required=True, unique=True)
    acronym = db.StringField(max_length=10, required=True, unique=True)
    fr_name = db.StringField(max_length=80, required=True, unique=True)
    government = db.BooleanField(required=True)
    display_order = db.IntField(min_value=0, max_value=100000, unique=True, required=True)
    list_level_twos = db.ListField(db.EmbeddedDocumentField(LevelTwo), default=[])

    def __repr__(self):
        return '<Client: %r>' % self.en_name



Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the DuplicateKeyError with the LevelOne and embedded LevelTwo models shown in the issue, then inspect how the embedded unique fields are indexed. Compare the behavior between mongoengine 0.8.6 and 0.10.6 and determine whether the requested per-parent uniqueness is supported; done means the expected behavior or required option is documented and covered by a regression test.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.