MongoEngine / MongoEngine/mongoengine
cascade_save does not save new referenced document
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with ReferenceField validation and the cascade=True save path described in the issue, using the User and UserSubscription example to trace where the unsaved reference is rejected. Determine the intended behavior for cascading new referenced documents and define tests showing whether the example saves successfully without breaking existing-reference behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100