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

cascade_save does not save new referenced document #1236

Open
lafrech opened this issue Feb 18, 2016 · 10 comments
Open

cascade_save does not save new referenced document #1236

lafrech opened this issue Feb 18, 2016 · 10 comments

Comments

@lafrech
Copy link
Member

@lafrech 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 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.

@celestian02
Copy link

@celestian02 celestian02 commented Apr 18, 2017

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

@NiklasRosenstein
Copy link

@NiklasRosenstein NiklasRosenstein commented Jun 25, 2017

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

@doaa-altarawy doaa-altarawy commented Oct 18, 2018

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

@shenin04 shenin04 commented Oct 24, 2018

Same issue here.

@ewengillies
Copy link

@ewengillies ewengillies commented Feb 8, 2019

+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 burchill commented Apr 7, 2020

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

@DonQueso89
Copy link
Contributor

@DonQueso89 DonQueso89 commented Apr 24, 2020

+1

@DataKinds
Copy link

@DataKinds 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

@Evisolpxe Evisolpxe commented Oct 12, 2020

+1

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

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.