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

Fix Dataservice.datasets sometimes non existent instead of being empty list #3242

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

magopian
Copy link
Contributor

@magopian magopian commented Jan 8, 2025

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 a ListField), then the field is unset (instead of set to be empty).

The hackish fix updates the datasets field to be [] in the post_save signal. This means that we might want to do the same thing for Dataset.resources, and maybe for other ListFields just in case.

We might want to follow another route, maybe subclassing MongoEngine.BaseDocument or MongoEngine.Document to overwrite the _delta method, or the MongoEngine.Document._get_update_doc method to not unset a field if it's a ListField.

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.

@maudetes maudetes self-requested a review January 8, 2025 17:18
Copy link
Contributor

@maudetes maudetes left a 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dataservice.save()
dataservice.save()
dataservice.reload()


# Setting it explicitely to a non-empty list should work as expected
dataservice.datasets = [dataset]
dataservice.save()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dataservice.save()
dataservice.save()
dataservice.reload()

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

Successfully merging this pull request may close these issues.

Enquêter sur la liste des datasets d'un dataservice inexistant plutôt que vide
2 participants