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 pydantic invalid when table=True(#1036) #1041

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

monchin
Copy link

@monchin monchin commented Aug 1, 2024

I found that when table=True is set, pydantic becomes invalid for usage like hero = Hero(name="Deadpond", secret_name="Dive Wilson", age="test"). As the document says you get all of Pydantic's features, including automatic data validation, serialization, and documentation. You can use SQLModel in the same way you can use Pydantic., I think table=True makes pydantic invalid is a bug, so I tried to fix it.

I delete a test case because this case is incompatible with pydantic. I also add a test case to ensure my code works. For current code, my new test case would fail, but it would pass for my new code.

raw_self = self.model_copy()
pydantic_validated_model = self.__pydantic_validator__.validate_python(
data,
self_instance=raw_self,

Choose a reason for hiding this comment

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

why was raw_self necessary here?

Copy link
Author

@monchin monchin Nov 27, 2024

Choose a reason for hiding this comment

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

why was raw_self necessary here?

self.__pydantic_validator__.validate_python(
                data,
                self_instance=raw_self,
            )

would update self, and something would go wrong on sqlmodel_table_construct for the changed self. Maybe that's the reason that the original code dosen't validate when table=True.

Actually, we need to get the pydantic-validated dict, and use this dict to update the original dict, and then construct tables by the updated-original dict.

Anyway, you can try self_instance=self, and some unittests would fail.

@iloveitaly
Copy link

Some related issues for this one:

However, I think this comment indicates that this is actually the desired behavior.

@monchin
Copy link
Author

monchin commented Nov 27, 2024

this is actually the desired behavior

I regard this action as a bug rather than a feature because sqlmodel announce that you get all of Pydantic's features, including automatic data validation, serialization, and documentation. You can use SQLModel in the same way you can use Pydantic. Models with no validation when table=True is definitely not the way we are using pydantic.

@muhammadyaseen
Copy link

Hi all, is there a plan to merge this? Currently facing the same issues discussed in #11 and #453 (and several other related issues) and none of the proposed workaround seem clean enough for my use case.

Thanks.

@monchin
Copy link
Author

monchin commented Dec 17, 2024

@tiangolo Would you please look at this PR snd see whether this solution is OK? It passes all tests.

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.

3 participants