Privileged issue
Issue Content
Currently alias parameter doesn't work as expected with Pydantic V2 installed.
Let's use this issue to track this.
👇 Here is a list of tests that showcase the problem (in the details):
Details
fromtypingimportType, UnionimportpytestfrompydanticimportVERSION, BaseModel, ValidationErrorfrompydanticimportFieldasPFieldfromsqlmodelimportField, SQLModel# -----------------------------------------------------------------------------------# ModelsclassPydanticUser(BaseModel):
full_name: str=PField(alias="fullName")
classSQLModelUser(SQLModel):
full_name: str=Field(alias="fullName")
# Models with config (validate_by_name=True)ifVERSION.startswith("2."):
classPydanticUserWithConfig(PydanticUser):
model_config= {"validate_by_name": True}
classSQLModelUserWithConfig(SQLModelUser):
model_config= {"validate_by_name": True}
else:
classPydanticUserWithConfig(PydanticUser):
classConfig:
allow_population_by_field_name=TrueclassSQLModelUserWithConfig(SQLModelUser):
classConfig:
allow_population_by_field_name=True# -----------------------------------------------------------------------------------# Tests# Test validate by name@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser])deftest_create_with_field_name(model: Union[Type[PydanticUser], Type[SQLModelUser]]):
withpytest.raises(ValidationError):
model(full_name="Alice")
@pytest.mark.parametrize("model", [PydanticUserWithConfig, SQLModelUserWithConfig])deftest_create_with_field_name_with_config(
model: Union[Type[PydanticUserWithConfig], Type[SQLModelUserWithConfig]],
):
user=model(full_name="Alice")
assertuser.full_name=="Alice"# Test validate by alias@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser, PydanticUserWithConfig, SQLModelUserWithConfig],)deftest_create_with_alias(
model: Union[
Type[PydanticUser],
Type[SQLModelUser],
Type[PydanticUserWithConfig],
Type[SQLModelUserWithConfig],
],
):
user=model(fullName="Bob") # using aliasassertuser.full_name=="Bob"# Test validate by name and alias@pytest.mark.parametrize("model", [PydanticUserWithConfig, SQLModelUserWithConfig])deftest_create_with_both_prefers_alias(
model: Union[Type[PydanticUserWithConfig], Type[SQLModelUserWithConfig]],
):
user=model(full_name="IGNORED", fullName="Charlie")
assertuser.full_name=="Charlie"# alias should take precedence# Test serialize@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser])deftest_dict_default_uses_field_names(
model: Union[Type[PydanticUser], Type[SQLModelUser]],
):
user=model(fullName="Dana")
data=user.dict()
assert"full_name"indataassert"fullName"notindataassertdata["full_name"] =="Dana"# Test serialize by alias@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser])deftest_dict_default_uses_aliases(
model: Union[Type[PydanticUser], Type[SQLModelUser]],
):
user=model(fullName="Dana")
data=user.dict(by_alias=True)
assert"fullName"indataassert"full_name"notindataassertdata["fullName"] =="Dana"# Test json by alias@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser])deftest_json_by_alias(
model: Union[Type[PydanticUser], Type[SQLModelUser]],
):
user=model(fullName="Frank")
json_data=user.json(by_alias=True)
assert ('"fullName":"Frank"'injson_data) or ('"fullName": "Frank"'injson_data)
assert"full_name"notinjson_dataWhen run with Pydantic V2:
t.py::test_create_with_field_name[PydanticUser] PASSED
t.py::test_create_with_field_name[SQLModelUser] FAILED
t.py::test_create_with_field_name_with_config[PydanticUserWithConfig] PASSED
t.py::test_create_with_field_name_with_config[SQLModelUserWithConfig] PASSED
t.py::test_create_with_alias[PydanticUser] PASSED
t.py::test_create_with_alias[SQLModelUser] FAILED
t.py::test_create_with_alias[PydanticUserWithConfig] PASSED
t.py::test_create_with_alias[SQLModelUserWithConfig] FAILED
t.py::test_create_with_both_prefers_alias[PydanticUserWithConfig] PASSED
t.py::test_create_with_both_prefers_alias[SQLModelUserWithConfig] FAILED
t.py::test_dict_default_uses_field_names[PydanticUser] PASSED
t.py::test_dict_default_uses_field_names[SQLModelUser] FAILED
t.py::test_dict_default_uses_aliases[PydanticUser] PASSED
t.py::test_dict_default_uses_aliases[SQLModelUser] FAILED
t.py::test_json_by_alias[PydanticUser] PASSED
t.py::test_json_by_alias[SQLModelUser] FAILED
All tests parameterized with both, Pydantic model and SQLModel model, to show the difference in behavior.
With Pydantic V1 all tests pass.
With Pydantic V2 most of tests fail with SQLModel model.
I would also expect that alias is used as a default value of column name. But it should be possible to override it by providing sa_column=Column("column_name") or sa_column_kwargs={"name": "column_name"}.
Any feedback is welcome. Please, review the set of tests, suggest use-cases that are currently not covered.
Would be nice to add set of tests for validation_alias and serialization_alias as well.
Privileged issue
Issue Content
Currently
aliasparameter doesn't work as expected with Pydantic V2 installed.Let's use this issue to track this.
👇 Here is a list of tests that showcase the problem (in the details):
Details
When run with Pydantic V2:
All tests parameterized with both, Pydantic model and SQLModel model, to show the difference in behavior.
With Pydantic V1 all tests pass.
With Pydantic V2 most of tests fail with SQLModel model.
I would also expect that
aliasis used as a default value of column name. But it should be possible to override it by providingsa_column=Column("column_name")orsa_column_kwargs={"name": "column_name"}.Any feedback is welcome. Please, review the set of tests, suggest use-cases that are currently not covered.
Would be nice to add set of tests for
validation_aliasandserialization_aliasas well.