-
-
Notifications
You must be signed in to change notification settings - Fork 664
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
There seems to be a bug in using model with aliased fields to define request body #374
Comments
I found that:
|
@boh5 Do you have solution about this problem? I use the latest version and I still have this problem. |
Facing the same issue here |
Would be amazing to get this addressed, if possible. It's blocking us from adopting this project. |
Looks like there is an open PR to fix this: #774 (comment) |
I think this PR will only partially fix this issue. It will allow a workaround using Do we know if this issue is on the radar to be addressed? |
Facing a similar issue here, the alias is not taken into account when generating the body for the request. I am also using pydantic's v2 |
I tried to work around the same issue but I ended with a similar bug. E.g.:
SQLModel is supposed to expand pydantic contract instead of superseding it. I hope for the prompt resolution because this bug prevents overcoming Python language limitations and limits API expression capabilities. |
Minimum Error Example: from sqlmodel import SQLModel, Field
from typing import Optional
class SimpleBookChapter(SQLModel):
class Config:
populate_by_name = True
chapter_number: int = Field(alias="episode_number")
title: Optional[str] = Field(alias="episode_title")
chapter_data = {"episode_number": 1, "episode_title": "Test Chapter"}
chapter = SimpleBookChapter.model_validate(chapter_data)
print(chapter) error log: chapter = SimpleBookChapter.model_validate(chapter_data)
File "D:\ProgramData\Anaconda3\envs\chapchat\lib\site-packages\sqlmodel\main.py", line 848, in model_validate
return sqlmodel_validate(
File "D:\ProgramData\Anaconda3\envs\chapchat\lib\site-packages\sqlmodel\_compat.py", line 314, in sqlmodel_validate
cls.__pydantic_validator__.validate_python(
pydantic_core._pydantic_core.ValidationError: 2 validation errors for SimpleBookChapter
chapter_number
Field required [type=missing, input_value={'episode_number': 1, 'ep..._title': 'Test Chapter'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.8/v/missing
title
Field required [type=missing, input_value={'episode_number': 1, 'ep..._title': 'Test Chapter'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.8/v/missing success example: from pydantic import Field
from sqlmodel import SQLModel
from typing import Optional
class SimpleBookChapter(SQLModel):
class Config:
populate_by_name = True
chapter_number: int = Field(alias="episode_number")
title: Optional[str] = Field(alias="episode_title")
chapter_data = {"episode_number": 1, "episode_title": "Test Chapter"}
chapter = SimpleBookChapter.model_validate(chapter_data)
print(chapter) success log:
|
It's work for me :) |
I was facing a similar problem where I wanted to have camel case fields for the endpoints, but snake case for the code and the database column. I solved overriding the Config class. from sqlmodel import Field, SQLModel
from humps import camelize
def to_camel(string):
return camelize(string)
class MyModel(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
my_field : str
class Config: # type: ignore
populate_by_name = True
alias_generator = to_camel It worked but it remains the problem with the typechecking ("Config" overrides symbol of same name in class "SQLModel") |
First Check
Commit to Help
Example Code
Description
Hero
model, thesecret_name
field aliased tosecretName
hero.secret_name
isNone
hero: Hero
tohero: HeroPydantic
, thehero.secret_name
can get correct value from post data. But the strainge thing is that I can get correcthero.secret_name
byhero: Hero = Hero.parse_obj(hero.dict(by_alias=True))
So it seems that alias argument in sqlmodel seems not working for converting body from post data, I have to write two models in order to achieve that.
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.7.6
Additional Context
No response
The text was updated successfully, but these errors were encountered: