There was a problem hiding this comment.
Note
Copilot was unable to run its full agentic suite in this review.
Adds support in SQLModel for Pydantic-style input population controls when validating models, enabling callers to explicitly choose whether aliases and/or field names are accepted during model_validate.
Changes:
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| tests/test_aliases.py | Adds regression tests for model_validate behavior with by_name and combined by_alias/by_name. |
| sqlmodel/main.py | Exposes by_alias and by_name on the public SQLModel.model_validate API and forwards them. |
| sqlmodel/_compat.py | Threads by_alias/by_name through sqlmodel_validate into Pydantic’s validator call. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong.
Description
SQLModel overrides model_validate, but the override does not accept or forward the by_alias and by_name arguments that Pydantic's model_validate supports.
SQLModel requires pydantic>=2.11, and both by_alias and by_name have been part of model_validate since Pydantic 2.11, so they are always available. Despite that, calling them on a SQLModel fails:
The plain Pydantic equivalent works, so this is a gap in SQLModel's override. It forces users with aliased fields to drop down to __pydantic_validator__ or restructure their input.
This is also reported in #1824.
Fix
Add by_alias and by_name to the model_validate override and to the internal sqlmodel_validate helper, forwarding them to validate_python. This mirrors how the other validation arguments (strict, from_attributes, context) are already handled, and keeps model_validate consistent with Pydantic.
No version gating is needed since the supported Pydantic floor (2.11) already includes both parameters.
Tests
Added parametrized tests in tests/test_aliases.py that run the same assertions against a plain Pydantic model and the equivalent SQLModel, covering by_name=True and the by_alias=True, by_name=True combination. They fail on main with the TypeError above and pass with this change.