BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
1.91k stars 201 forks source link

[BUG] `save_changes()` doesn't throw an error when document doesn't exists #421

Closed Luc1412 closed 1 year ago

Luc1412 commented 1 year ago

Describe the bug In 1.11.9 state was changed and always defaults being not None, instead it got a dict with the default values. This causes check_if_state_saved to never throw an error.

save_changes on a new created document that isn't saved in the database silently does nothing.

To Reproduce

user_data = UserEntry(...)
user_data.x = ...
await user_data.save_changes()

Expected behavior StateNotSaved("No state was saved") Additional context https://github.com/roman-right/beanie/compare/1.11.8...1.11.9 https://canary.discord.com/channels/822196934973456394/822196935435747332/1042243293662158970

guesswh0 commented 1 year ago

This looks wrong when you expecting from save_changes() creating new documents. But if document existed and you trying to save changes (e.g. some race condition deleted doc) this have to happen.

Luc1412 commented 1 year ago

In my case I got a custom fetch document that either finds the document by _id or creates a new document if not found.

To save changes, I only use save_changes() so I don't know weather the document is fetched from db or not.

So I subclassed Document to overwrite save_changes() to insert the doc if it was just created.

Currently save() inserts the document too, if newly created, so it might make sense to implement that for save_changes() too. Otherwise at least something should happen when saving_changes on a non existent doc e.g. an exception should be raised.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been stalled for 14 days with no activity.