pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/fastapi/sqlmodel/pull/1728/files

ia="all" rel="stylesheet" href="https://github.githubassets.com/assets/code-2d31826944fd3be8.css" /> 🏷️ Allow `AliasChoices` and `AliasPath` for `validation_alias` in `Field` by YuriiMotov · Pull Request #1728 · fastapi/sqlmodel · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sqlmodel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
__version__ = "0.0.35"

# Re-export from Pydantic
from pydantic import AliasChoices as AliasChoices
from pydantic import AliasPath as AliasPath

# Re-export from SQLAlchemy
from sqlalchemy.engine import create_engine as create_engine
from sqlalchemy.engine import create_mock_engine as create_mock_engine
Expand Down
10 changes: 5 additions & 5 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
overload,
)

from pydantic import BaseModel, EmailStr
from pydantic import AliasChoices, AliasPath, BaseModel, EmailStr
from pydantic.fields import FieldInfo as PydanticFieldInfo
from sqlalchemy import (
Boolean,
Expand Down Expand Up @@ -240,7 +240,7 @@ def Field(
*,
default_factory: NoArgAnyCallable | None = None,
alias: str | None = None,
validation_alias: str | None = None,
validation_alias: str | AliasPath | AliasChoices | None = None,
serialization_alias: str | None = None,
title: str | None = None,
description: str | None = None,
Expand Down Expand Up @@ -283,7 +283,7 @@ def Field(
*,
default_factory: NoArgAnyCallable | None = None,
alias: str | None = None,
validation_alias: str | None = None,
validation_alias: str | AliasPath | AliasChoices | None = None,
serialization_alias: str | None = None,
title: str | None = None,
description: str | None = None,
Expand Down Expand Up @@ -335,7 +335,7 @@ def Field(
*,
default_factory: NoArgAnyCallable | None = None,
alias: str | None = None,
validation_alias: str | None = None,
validation_alias: str | AliasPath | AliasChoices | None = None,
serialization_alias: str | None = None,
title: str | None = None,
description: str | None = None,
Expand Down Expand Up @@ -368,7 +368,7 @@ def Field(
*,
default_factory: NoArgAnyCallable | None = None,
alias: str | None = None,
validation_alias: str | None = None,
validation_alias: str | AliasPath | AliasChoices | None = None,
serialization_alias: str | None = None,
title: str | None = None,
description: str | None = None,
Expand Down
33 changes: 32 additions & 1 deletion tests/test_aliases.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest
from pydantic import AliasChoices as PAliasChoices
from pydantic import AliasPath as PAliasPath
from pydantic import BaseModel, ValidationError
from pydantic import Field as PField
from sqlmodel import Field, SQLModel
from sqlmodel import AliasChoices, AliasPath, Field, SQLModel

"""
Alias tests for SQLModel and Pydantic compatibility
Expand Down Expand Up @@ -95,10 +97,18 @@ def test_json_by_alias(

class PydanticUserV2(BaseModel):
first_name: str = PField(validation_alias="firstName", serialization_alias="f_name")
second_name: str | None = PField(
default=None, validation_alias=PAliasChoices("secondName", "surname")
)
nickname: str | None = PField(default=None, validation_alias=PAliasPath("names", 2))


class SQLModelUserV2(SQLModel):
first_name: str = Field(validation_alias="firstName", serialization_alias="f_name")
second_name: str | None = Field(
default=None, validation_alias=AliasChoices("secondName", "surname")
)
nickname: str | None = Field(default=None, validation_alias=AliasPath("names", 2))


@pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2])
Expand All @@ -109,6 +119,27 @@ def test_create_with_validation_alias(
assert user.first_name == "John"


@pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2])
def test_create_with_validation_alias_alias_choices(
model: type[PydanticUserV2] | type[SQLModelUserV2],
):
user = model.model_validate({"firstName": "John", "secondName": "Doe"})
assert user.second_name == "Doe"

user2 = model.model_validate({"firstName": "John", "surname": "Doe"})
assert user2.second_name == "Doe"


@pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2])
def test_create_with_validation_alias_alias_path(
model: type[PydanticUserV2] | type[SQLModelUserV2],
):
user = model.model_validate(
{"firstName": "John", "names": ["John", "Doe", "Johnny"]}
)
assert user.nickname == "Johnny"


@pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2])
def test_serialize_with_serialization_alias(
model: type[PydanticUserV2] | type[SQLModelUserV2],
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy