MongoEngine / MongoEngine/mongoengine

NotUniqueError on field with unique=False

Open
#2,501 0 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, I'm facing the following situation:

I have two classes:

1:EmbeddedDocument
and the other is a
2: Document

Inside of the document I'm using a variable called products which is an EmbeddedDocumentListField to store objects of class 1, and on class 1 I'm having a primary key that uses an ObjectIdField, now the issue happens when I create two or more objects where the variable products = [] (when it's an empty list), in that situation I'm receiving a NotUniqueError, even when products is defined as non-unique.

The code:
class Image(EmbeddedDocument):
    """ Product with image schema """
    oid = ObjectIdField(required=True, default=ObjectId, unique=True, primary_key=True)
    image_url = StringField(required=True)


class ImageLibrary(Document):
    """ Library of images"""
    store_id = StringField(primary_key=True)

    products = EmbeddedDocumentListField("Image", unique=False)

    meta = {'db_alias': 'CMS_DB', 'allow_inheritance': True, 'collection': 'image_libraries'}
Testing:
image_library = {"store_id":store_id, "products":[]}
image_library = ImageLibrary(**image_library).save() # Saves correctly and returns the object with an ID

image_library = {"store_id":store_id, "products":[]}
image_library = ImageLibrary(**image_library).save() # Produces the error
The error:

mongoengine.errors.NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error collection: 5c429_CMS_DEV.images_libraries index: products._id_1 dup key: { products._id: null }, full error: {'index': 0, 'code': 11000, 'keyPattern': {'products._id': 1}, 'keyValue': {'products._id': None}, 'errmsg': 'E11000 duplicate key error collection: 5c429_CMS_DEV.images_libraries index: products._id_1 dup key: { products._id: null }'})

There's any workaround for this? I understand that the problem is happening because the EmbeddedDocument class 1 has a unique object_id inside of it, but it should be independent of the EmbeddedDocumentListField which is non-unique.

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 running the two-class reproducer against MongoDB and inspect MongoEngine's index creation and validation paths for EmbeddedDocumentListField with a primary-key ObjectIdField. Trace why an empty products list creates a unique products._id index, then verify that saving two ImageLibrary documents with empty products no longer raises NotUniqueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.