Skip to content

cascade_save does not save new referenced document #1236

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

Open
lafrech opened this issue Feb 18, 2016 · 14 comments · Fixed by nickfrev/mongoengine#1 · May be fixed by #2615
Open

cascade_save does not save new referenced document #1236

lafrech opened this issue Feb 18, 2016 · 14 comments · Fixed by nickfrev/mongoengine#1 · May be fixed by #2615

Comments

@lafrech
Copy link
Member

lafrech commented Feb 18, 2016

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.

@Kobnar
Copy link

Kobnar commented May 11, 2016

I have the same problem. doc.save(cascade=True) does not save any documents unless they already exist, and doc.cascade_save() does not save the document itself.

@celestianx
Copy link

Hi. Any news on this ? It seems like the bug is still there.

@NiklasRosenstein
Copy link

Get the same behaviour here with MongoEngine 0.11.0.

  • save(cascade=True) doesn't actually seem to do cascade saving (still gives the same errors as without cascade=True)
  • save_cascade() does not save the document itself

sato-s added a commit to sato-s/mongoengine that referenced this issue Jul 29, 2017
@doaa-altarawy
Copy link

I have the same problem reported here, save(cascade=True) doesn't work and thus saving the parent document throws a ValidationError.

@shenin04
Copy link

Same issue here.

@ewengillies
Copy link

+1 bumping this, it would be great to be able to save new embedded documents as references without having to dig into the substructure of the parent document to save all the new objects.

@burchill
Copy link

burchill commented Apr 7, 2020

+1 bumping this. How is this still open after four years?

@DonQueso89
Copy link
Contributor

+1

@DataKinds
Copy link

DataKinds commented Jun 24, 2020

+1 once again. Having to override the save method on all my classes is a massive PITA. Here's the method that I usually use if anyone wants it. Replace self.refs with your ReferenceFields.

def save(self, *args, **kwargs):
    for ref in self.refs.values():
        ref.save()
    return super().save(*args, **kwargs)

@Evisolpxe
Copy link

+1

2 similar comments
@OliverLSanz
Copy link

+1

@joeriddles
Copy link

+1

nickfrev added a commit to nickfrev/mongoengine that referenced this issue Dec 14, 2021
Save the children documents first to avoid the issue where a parent cannot save due to having new children documents.
@nickfrev nickfrev linked a pull request Jan 7, 2022 that will close this issue
@mo-cmyk
Copy link

mo-cmyk commented Oct 21, 2022

Is this Issue solved?

@NisugaJ
Copy link

NisugaJ commented Oct 21, 2023

Is this solved?

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