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

Calling patch with no changes will fail #355

Open
armanddidierjean opened this issue Mar 2, 2024 · 1 comment
Open

Calling patch with no changes will fail #355

armanddidierjean opened this issue Mar 2, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@armanddidierjean
Copy link
Member

Subject of the issue

If we call an update crud without providing any change, an error will be thrown.

Proposed solution

    if not any(recommendation.model_dump().values()):
        return

    result = await db.execute(
        update(models_recommendation.Recommendation)
        .where(models_recommendation.Recommendation.id == recommendation_id)
        .values(**recommendation.model_dump(exclude_none=True))
    )
    if result.rowcount == 1:
        await db.commit()
    else:
        await db.rollback()
        raise ValueError

See @julien4215 comment

@armanddidierjean armanddidierjean added the bug Something isn't working label Aug 22, 2024
@armanddidierjean
Copy link
Member Author

Its seems the proposed solution does not work if some fields are set to False.

According to pydantic/pydantic#4613 we could use:

if not bool(
    recommendation.model_fields_set
):

If we need to exclude some fields from the model, we can use recommendation.model_fields_set - {"field_to_remove"},

Rotheem pushed a commit that referenced this issue Aug 23, 2024
)

to prevent `False` values to be interpreted as not provided fields

Related to #355
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

1 participant