🐛 Forward by_alias and by_name to model_validate#1977
Open
ATOM00blue wants to merge 1 commit into
Open
Conversation
SQLModel overrides model_validate but did not pass through the by_alias and by_name arguments that Pydantic supports. Since SQLModel requires pydantic>=2.11, where both are always available, calling Model.model_validate(data, by_alias=True) (or by_name=True) raised TypeError: unexpected keyword argument. Add both parameters to the model_validate override and to sqlmodel_validate, forwarding them to validate_python.
There was a problem hiding this comment.
Pull request overview
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:
- Extended
SQLModel.model_validate(...)to acceptby_aliasandby_namekeyword arguments and forward them to the underlying validator. - Extended the internal
sqlmodel_validate(...)compatibility layer to passby_alias/by_namethrough to__pydantic_validator__.validate_python(...). - Added tests covering default behavior vs
by_name=True, and combinedby_alias=True, by_name=True.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| 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.
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
SQLModel overrides
model_validate, but the override does not accept or forward theby_aliasandby_namearguments that Pydantic'smodel_validatesupports.SQLModel requires
pydantic>=2.11, and bothby_aliasandby_namehave been part ofmodel_validatesince 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_aliasandby_nameto themodel_validateoverride and to the internalsqlmodel_validatehelper, forwarding them tovalidate_python. This mirrors how the other validation arguments (strict,from_attributes,context) are already handled, and keepsmodel_validateconsistent 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.pythat run the same assertions against a plain Pydantic model and the equivalent SQLModel, coveringby_name=Trueand theby_alias=True, by_name=Truecombination. They fail onmainwith theTypeErrorabove and pass with this change.