Skip to content

🐛 Fix Pydantic alias bug #1379

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

STocidlowski
Copy link

@STocidlowski STocidlowski commented May 14, 2025

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.
Copy link

@jrealpe jrealpe left a comment

Choose a reason for hiding this comment

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

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

@svlandeg svlandeg added the bug Something isn't working label Jun 23, 2025
@svlandeg svlandeg changed the title Pydantic 'alias' bug 🐛 Fix Pydantic alias bug Jun 23, 2025
@svlandeg
Copy link
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)?

@gillespilloudkerlink
Copy link

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))

@gillespilloudkerlink
Copy link

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

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.

4 participants