Skip to content

Cascading save of Reference ListField fails #2023

@AngryUbuntuNerd

Description

@AngryUbuntuNerd

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions