Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascading save of Reference ListField fails #2023

Open
AngryUbuntuNerd opened this issue Mar 18, 2019 · 2 comments · Fixed by nickfrev/mongoengine#1 · May be fixed by #2615
Open

Cascading save of Reference ListField fails #2023

AngryUbuntuNerd opened this issue Mar 18, 2019 · 2 comments · Fixed by nickfrev/mongoengine#1 · May be fixed by #2615

Comments

@AngryUbuntuNerd
Copy link

AngryUbuntuNerd commented Mar 18, 2019

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)
@AngryUbuntuNerd AngryUbuntuNerd changed the title Recursive cascading save fails Cascading save of Reference ListField fails Mar 25, 2019
@AngryUbuntuNerd
Copy link
Author

I simplified the example and changed text + title, as I realized that recursive saving is not the problem, but the ListField(ReferenceField(...))

@DataKinds
Copy link

DataKinds commented Jun 24, 2020

This is a duplicate of #28 and #1236.... issues that have been open since 2012 and 2016 respectively...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants