MongoEngine / MongoEngine/mongoengine

Cascading save of Reference ListField fails

Open
#2,023 2 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

When I have two models that are linked to each other via a One-To-Many list of references, a cascading save seems to not work.

Run the following code and observe in your database how User is saved, but Comment is not. In other scenarios I have seen this to fail with a ValidationError (because the reference cant be resolved), but in this example it seems to fail silently.

I have tried using the setting "cascade": True on the "meta" variable of the document instead, but the effect is the same.

from typing import List
from uuid import UUID, uuid4

from mongoengine import Document, UUIDField, ReferenceField, StringField, ListField, connect

connect(host='mongodb://localhost/docker')


class Comment(Document):
    id: UUID = UUIDField(primary_key=True, default=uuid4)
    text = StringField(required=True)


class User(Document):
    id: UUID = UUIDField(primary_key=True, default=uuid4)
    comments: List[Comment] = ListField(ReferenceField('Comment'))


comment1 = Comment(text="hello world")
comment2 = Comment(text="goodbye world")
user = User(comments=[comment1, comment2])

user.save(cascade=True)

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 supplied Python example against MongoDB and inspect the save(cascade=True) behavior for User.comments, ListField, and ReferenceField. Done means the User and both Comment documents are persisted consistently, without the silent failure or unresolved-reference ValidationError described.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.