-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Open
nickfrev/mongoengine
#1Description
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
Labels
No labels