MongoEngine / MongoEngine/mongoengine

`save(cascade=True)` doesn't work with `DynamicDocument` without fields?

Open
#1,897 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

from mongoengine import *
connect('naruto')


class User(DynamicDocument):
    pass

class Address(DynamicDocument):
    pass

if __name__ == '__main__':
    User.drop_collection()
    Address.drop_collection()

    address = Address()
    address.village = 'Konohagakure'

    # address.save()  # I don't want to have to do this

    naruto = User()
    naruto.name = 'Uzumaki Naruto'
    naruto.title = '7th Hokage'
    naruto.address = address

    # naruto.cascade_save()
    naruto.save(cascade=True)

Doing the above results in mongoengine.errors.OperationError: Only saved documents can have a valid dbref

naruto.cascade_save() succeeds but doesn't save anything.


I don't want to do an object.save() for every new object that get's created. Some of my objects can create new objects on a method call, and I only wish to do a collection.save() once all the objects have been created.

Additionally, I'm looking for complete schema-lessness, so everything is a DynamicDocument with no fields defined.

Is there any way of accomplishing this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example with DynamicDocument, then trace the save(cascade=True) and cascade_save entry points around the unsaved address and its DBRef error. Done means a single save can persist newly created schema-less related objects without requiring each object to be saved first, with regression coverage for this example.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.