MongoEngine / MongoEngine/mongoengine

update(add_to_set) on a ListField(CachedReferenceField) only includes the _id

Open
#1,560 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

Modifying existing CachedReferenceField test code to test a ListField(CachedReferenceField()) using doc.update(add_to_set__animal=animal_instance) results in the _id being in the list. None of the cached fields made it.

from mongoengine import *
from mongoengine.connection import get_db
from mongoengine.base import (BaseDict, BaseField, EmbeddedDocumentList,
                              _document_registry)



class Owner(EmbeddedDocument):
    name = StringField()
    tags = ListField(StringField())

class Animal(Document):
    name = StringField()
    tag = StringField()

    owner = EmbeddedDocumentField(Owner)

class Ocorrence(Document):
    person = StringField()
    animal = ListField(CachedReferenceField(
        Animal, fields=['tag', 'owner.tags']))

Animal.drop_collection()
Ocorrence.drop_collection()

a = Animal(name="Leopard", tag="heavy",
           owner=Owner(tags=['cool', 'funny'],
                       name="Wilson Júnior")
           )
a2 = Animal(name="Tiger", tag="too heavy",
           owner=Owner(tags=['cool2', 'funny2'],
                       name="Wilson")
           )
a.save()
a2.save()
o = Ocorrence(person="teste 2")
o.save()
o.update(add_to_set__animal=a)
o.update(add_to_set__animal=a2)

# doing it this way works as documented
#o = Ocorrence(person="teste 2", animal=[a,a2])
#o.save()

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 reproduction using the Owner, Animal, and Ocorrence definitions shown in the issue, comparing update(add_to_set__animal=animal_instance) with saving a populated list. Trace how the CachedReferenceField values are serialized during the add_to_set update. Done means both cached fields, including nested owner.tags, remain available in the stored list and the regression is covered by a test.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.