-
Notifications
You must be signed in to change notification settings - Fork 89
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
Fix Dataservice.datasets sometimes non existent instead of being empty list #3242
base: master
Are you sure you want to change the base?
Fix Dataservice.datasets sometimes non existent instead of being empty list #3242
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the test and the explanation 🙏
Seeing as it can happen on any ListField, I think it will be difficult to maintain to add this custom logic to all the post_save
places?
Maybe we should be resilient with the ListField being unset? We're currently not resilient on resource upload and dataservice filtering on no datasets.
# See https://github.com/MongoEngine/mongoengine/issues/267#issuecomment-283065318 | ||
# Setting it explicitely to an empty list should NOT remove the field. | ||
dataservice.datasets = [] | ||
dataservice.save() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dataservice.save() | |
dataservice.save() | |
dataservice.reload() |
|
||
# Setting it explicitely to a non-empty list should work as expected | ||
dataservice.datasets = [dataset] | ||
dataservice.save() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dataservice.save() | |
dataservice.save() | |
dataservice.reload() |
…ing empty list" This reverts commit fbe7518.
…issing ListFields
Fixes #1430
The main issue seems to be because of a bug in mongoengine.
This first draft is a hackish fix: when a
ListField
is updated, if it's changed to be the empty list (or if the result is an empty list, if you dropped the last element for example), instead of being set to[]
, it's in fact unset (the field is removed from the document).The added test showcases this.
This happens in BaseDocument._delta: if the value isn't different from the default value (which is
[]
by default on aListField
), then the field is unset (instead of set to be empty).The hackish fix updates the
datasets
field to be[]
in thepost_save
signal. This means that we might want to do the same thing forDataset.resources
, and maybe for otherListField
s just in case.We might want to follow another route, maybe subclassing
MongoEngine.BaseDocument
orMongoEngine.Document
to overwrite the_delta
method, or theMongoEngine.Document._get_update_doc
method to not unset a field if it's aListField
.For the record, there's an existing opened PR on mongoengine (that I didn't review, so no clue if it fixes it properly), but it's been open for 3 years already, so not sure if or when it'll be merged.