FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

馃悰 Fix Pydantic `alias` bug by STocidlowski 路 Pull Request #1379 路 fastapi/sqlmodel 路 GitHub

Repository navigation

馃悰 Fix Pydantic alias bug - #1379

Closed
STocidlowski wants to merge 3 commits into
fastapi:mainfrom
STocidlowski:patch-1
Closed

STocidlowski wants to merge 3 commits into
fastapi:mainfrom
STocidlowski:patch-1

Conversation

STocidlowski commented May 14, 2025 •
edited
Loading

Copy link
Copy Markdown

bug

/This workaround fixes Pydantic serialization/deserialization while not modifying the internal alias logic. I cannot figure out why alias is not being passed through anymore.

For example, I was needing to add schema_extra with these key values in order to read/write data from a Json

https://docs.pydantic.dev/latest/concepts/fields/#field-aliases
#290

Thank you for your consideration.

STocidlowski and others added 2 commits May 14, 2025 12:40
This workaround fixes Pydantic serialization/deserialization while not modifying the internal alias logic. I cannot figure out why alias is not being passed through anymore.

For example, I was needing to add schema_extra with these key values in order to read data from a Json with different field names.

Thank you for your consideration.

jrealpe left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. I tested it on a SQLModel and it works fine. It keeps the syntax clean by using the same alias field.

svlandeg added the bug Something isn't working label Jun 23, 2025
svlandeg changed the title Pydantic 'alias' bug 馃悰 Fix Pydantic alias bug Jun 23, 2025

Copy link
Copy Markdown
Member

Hi @STocidlowski, thanks for the contribution! Do you have a minimal reproducible example that shows the bug on main (but has it fixed with this PR)?

Copy link
Copy Markdown

Thanks for this useful fix. It work for my usecase :

Reproductible example :

from sqlmodel import Field, Session, SQLModel, create_engine, select


class Hero(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str
    secret_name: str = Field(..., alias="myLittleSecretAlias")  # Using alias for JSON serialization
    age: int | None = None


sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

engine = create_engine(sqlite_url, echo=True)

SQLModel.metadata.create_all(engine)

with Session(engine) as session:

    hero_1 = Hero(name="Deadpond", myLittleSecretAlias="Dive Wilson")
    session.add(hero_1)


    statement = select(Hero).where(Hero.name == "Deadpond")
    hero = session.exec(statement).one_or_none()

    # without the fix the result is :
    #  {'name': 'Deadpond', 'secret_name': 'Dive Wilson', 'id': 1, 'age': None}
    # with the fix it is :
    #  {'name': 'Deadpond', 'myLittleSecretAlias': 'Dive Wilson', 'id': 1, 'age': None}
    print(hero.model_dump(by_alias=True))

Copy link
Copy Markdown

a workaround is the usage of schema_extra={"serialization_alias": "metadata"} instead of the "alias" parameter.

svlandeg self-assigned this Sep 16, 2025

Copy link
Copy Markdown
Member

See also: #1577

Copy link
Copy Markdown
Member

Thanks @STocidlowski, @jrealpe and @gillespilloudkerlink for your contributions here. I'll go ahead and close this PR in favor of #1577 which is more extensive. But thanks again! 馃檹

svlandeg closed this Oct 21, 2025
svlandeg removed their assignment Oct 21, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL