MongoEngine / MongoEngine/mongoengine

Cannot handle indexes on GenericReferenceField

Open
#2,628 1 comment 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

A document with the following structure will crash MongoEngine 0.24.0

class MyDoc(Document):
    fld = GenericReferenceField()
    meta = {
            "indexes": [{"fields": ["fld._ref.$id"]}]
    }

This type of index was fine before, but now throws an error:

...
File ".../mongoengine/base/metaclasses.py", line 370, in __new__
    meta["index_specs"] = new_class._build_index_specs(meta["indexes"])
  File ".../mongoengine/base/document.py", line 848, in _build_index_specs
    index_specs = [cls._build_index_spec(spec) for spec in meta_indexes]
  File ".../mongoengine/base/document.py", line 848, in <listcomp>
    index_specs = [cls._build_index_spec(spec) for spec in meta_indexes]
  File ".../mongoengine/base/document.py", line 941, in _build_index_spec
    fields = cls._lookup_field(parts)
  File ".../mongoengine/base/document.py", line 1143, in _lookup_field
    raise LookUpError(
mongoengine.errors.LookUpError: Cannot perform join in mongoDB: fld___ref__$id

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

Reproduce the documented MyDoc and index definition on MongoEngine 0.24.0, then start in mongoengine/base/document.py at _build_index_spec and _lookup_field, as identified by the traceback. Trace how fld._ref.$id is parsed and verify that the same document definition no longer raises LookUpError and accepts the index specification.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.