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

[BUG] before_event not working with validate_on_save = True #894

Open
ErikvdVen opened this issue Mar 7, 2024 · 2 comments
Open

[BUG] before_event not working with validate_on_save = True #894

ErikvdVen opened this issue Mar 7, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ErikvdVen
Copy link

ErikvdVen commented Mar 7, 2024

I'm using this class to implement the ManifestVersion type in my application: https://python-semver.readthedocs.io/en/latest/advanced/combine-pydantic-and-semver.html

The problem is that Beanie saves this value as an array in the database and when loading the document from the database, it is not able to convert it back, ie. it's not able to load that document again.

I fixed this by triggering the @before_event decorator, though not ideal, it works. But, it doesn't work when validate_on_save=True is set. It seems that validate_on_save is triggered after @before_event.

class UserModel(BeanieDocument):
    name: str
    version: ManifestVersion 

    @before_event(Insert, Replace)
    def semver_to_string(self):
        self.version = str(self.version
  
    class Settings:
       validate_on_save = True

This wouldn't be an issue for me if there is a possibility to tell Beanie explicitly to convert certain datatypes when inserting/replacing them in the database (after validation). Now it decides that some objects should be an array, while it works better when stored as string.

Copy link

github-actions bot commented Apr 7, 2024

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

@github-actions github-actions bot added the Stale label Apr 7, 2024
@roman-right roman-right added bug Something isn't working and removed Stale labels Apr 16, 2024
@roman-right
Copy link
Member

Hi! Thank you for the report. I'll check it during the next bug fixing session

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants