-
-
Notifications
You must be signed in to change notification settings - Fork 715
[Question] Why are pydantic validation results ignored for models with table=True
? Where are the benefits?
#406
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
Comments
Even if a add a custom validator (below) to the classes the outcome does not change. Only the error message from pydantic changes to @validator("value", pre=True)
@classmethod
def valid_value(cls, value: t.Any) -> EnumValues: # noqa: ANN401
if isinstance(value, EnumValues):
return value
if isinstance(value, str):
if value.upper() in (e.name for e in EnumValues):
return EnumValues[value.upper()]
raise ValueError("Invalid value") |
The issue is much worse as it seems as no validation is running on creation of class Table2(SQLModel, table=True):
id: int | None = Field(default=None, nullable=False, primary_key=True) # noqa: VNE003
value: str
try:
print(Table2()) # id=None
except ValidationError as e:
print(e) # NEVER REACHED -- should be: field required (type=value_error.missing) |
The validation is ignored when the model is a table: https://github.com/tiangolo/sqlmodel/blob/main/sqlmodel/main.py#L499-L504 But I did not see it being mentioned in the docs. But why are errors ignored? I cannot see any benefit for this rather only downsides, because the check runs anyways. So why hide the result? |
table=True
table=True
? Where are the benefits?
table=True
? Where are the benefits?table=True
? Where are the benefits?
After some thinking I came to the conclusion that maybe the validation error raising for Thinking experiment:When I have an SQLModel with This also applies the other way around. If I for example don't use a "Create" model for prior data-validation in a FastAPI route and simply create a ProposalI would propose to add a config switch that changes the validation silencing behavior. I would further propose to state the current behavior in the docs. |
Since this is explicitly worded as a question, maybe you are aware but if not see issue (and discussion) #52 |
Thanks for sharing. Actually I was not aware of the linked issue. As you can see in the feed above the issue was initially not a question, but more like a bug report. I guess this is now a duplicate. Gonna close it. |
First Check
EDIT: See #406 (comment)
Commit to Help
Example Code
Description
table=True
with a valid string in the enum -> success (as it should be)table=True
with an invalid string in the enum -> error (as it should be)table=True
with a valid string in the enum -> success (as it should be)table=True
with an invalid string in the enum -> success (should be error, but creates an instance which is totally missing the non-optional and non-defaultvalue
field)Operating System
Linux
Operating System Details
Ubuntu
SQLModel Version
0.0.6
Python Version
3.10.4
Additional Context
No response
The text was updated successfully, but these errors were encountered: