Skip to content

cascade_save does not save new referenced document #1236

Open
nickfrev/mongoengine
#1
@lafrech

Description

@lafrech

When saving a document with a ReferenceField holding a new object, I get an error:

mongoengine.errors.ValidationError: ValidationError 
You can only reference documents once they have been saved to the database

I naively expected cascade=True to automatically save the new document, while apparently it only automatically saves changes to existing documents.

Is this something that could be changed or is it meant to be for good reasons?

Example:

    class User(Document):
        name = StringField()

    class UserSubscription(Document):
        name = StringField()
        user = ReferenceField(User, dbref=False)

    User.drop_collection()
    UserSubscription.drop_collection()

    # u1 = User(name="Ross").save()
    u1 = User(name="Ross")

    sub = UserSubscription(user=u1).save()

I'd like this not to throw ValidationError but to save u1 in cascade.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions