-
-
Notifications
You must be signed in to change notification settings - Fork 678
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
base: main
Are you sure you want to change the base?
Conversation
raw_self = self.model_copy() | ||
pydantic_validated_model = self.__pydantic_validator__.validate_python( | ||
data, | ||
self_instance=raw_self, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Some related issues for this one:
However, I think this comment indicates that this is actually the desired behavior. |
I regard this action as a bug rather than a feature because |
@tiangolo Would you please look at this PR snd see whether this solution is OK? It passes all tests. |
I found that when
table=True
is set, pydantic becomes invalid for usage likehero = Hero(name="Deadpond", secret_name="Dive Wilson", age="test")
. As the document saysyou 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 thinktable=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.