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

Document.save not saving the doc #1246

Closed
knoxxs opened this issue Mar 2, 2016 · 3 comments
Closed

Document.save not saving the doc #1246

knoxxs opened this issue Mar 2, 2016 · 3 comments

Comments

@knoxxs
Copy link

knoxxs commented Mar 2, 2016

Storing an instance with only primary_key set doesn't store the document. If any other field is set apart from primary_key the document is getting stored.

This must be happening as mongoengine assuming it to be update and as the instance has no other data set, hence mongoengine is getting northing in updates and removals.

Eg:

class Sample(Document):
    key = StringField(primary_key=True)
    count = IntField()

sample = Sample()
sample.key = "123"
sample.save() # does nothing

sample.count = 0
sample.save() # saves the document

I know I can use force_insert in save to make it work. but is this the desired behaviour?

@lafrech lafrech added the Bug label Mar 22, 2016
@vintozver
Copy link

primary_key functionality must be revised.
mongodb has "_id" as a primary key. https://docs.mongodb.org/manual/core/index-single/#index-type-id
Thus, db_field parameter (which is default to the name of the field) is invalid in context when primary_key is True.

Also, keys may be compound, and if having two fields with primary_key in the same document, should it be considered as a compound key or invalid document definition?

Clarifications are necessary to proceed.

@amcgregor
Copy link
Contributor

amcgregor commented Jun 23, 2016

MongoDB allows for compound primary keys. Store an embedded document. There are very specific concerns relating to ensuring the order of the nested values is consistent between documents (or you lose out on sane ordering and range querying), and exact matching on the _id field requiring a fully populated sub-document, but. Two fields marked primary_key is entirely reasonable. One of our analytics collections, as an example, has an 8 way primary key. (Time-sliced unique combination of company, invoice, job, site, user, and package…)

On the actual issue of this ticket: if any ID is present, MongoEngine has no way of knowing (without performing additional, potentially expensive queries) that the document is actually new and not present in the DB. force_insert is required in this situation, since "save" has multiple meanings. One of the reasons I'm slowly migrating away from this style of "active record"… the ambiguities and edge cases just make things confusing and complicated. (The bare PyMongo driver does not suffer this problem, with a clear separation between .update_one/many and .insert_one/many.)

@wojcikstefan
Copy link
Member

Agreed with everything @amcgregor said.

There's not much we can do here given that we don't know whether a document with a given ID already exists or not. Please use QuerySet.objects.insert(doc) or doc.save(force_insert=True).

@wojcikstefan wojcikstefan changed the title save not saving the doc Document.save not saving the doc Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants