🐛 Fix `alias` support for Pydantic v2
Description
This PR fixes the Field(alias="...") functionality that was broken in Pydantic v2 while maintaining full backward compatibility with Pydantic v1.
Problem
In Pydantic v2, the alias parameter in SQLModel's Field function was not working correctly for validation and serialization. The issue was that Pydantic v2 requires explicit validation_alias and serialization_alias parameters, but SQLModel wasn't automatically propagating the alias value to these parameters.
Solution
Changes Made:
-
Enhanced Field function:
- Added
validation_aliasandserialization_aliasparameters - Implemented version-specific parameter handling
- Added
-
Automatic alias propagation:
- ~~Modified
post_init_field_infoin_compat.pyto automatically setvalidation_aliasandserialization_aliasfromaliaswhen not explicitly provided (Pydantic v2 only)~~ UPD YuriiMotov: updated it to just usingaliasvalue forvalidation_aliasandserialization_aliasif they are not specified directly
- ~~Modified
-
Backward compatibility:
- Filter out v2-only parameters from FieldInfo in Pydantic v1
- Raise clear RuntimeError when v2 features are used with v1
- Conditional test execution based on Pydantic version
-
Comprehensive testing:
- Added extensive test coverage for both Pydantic v1 and v2
- Tests automatically skip v2-specific features when running with v1
- Covers all alias scenarios including edge cases
Breaking Changes None. This is purely a bug fix with backward compatibility maintained.
Closes Fixes #1536
Hope you don't mind I submitted this review
Of course not! I tentatively assign PRs when I want to review them in the near future, just to keep track of them, but sometimes other tasks come in between 🙈 Feel free to review whatever you want!
@YuriiMotov @svlandeg I’ve addressed all the comments from the review. Let me know if there’s anything else you’d like me to focus on.
@YuriiMotov I have made changes based on your suggestion.
schema_extra. Precedence is: explicit params > schema_extra > alias propagation. In v1, using these keys raises a RuntimeError. No warnings added.
Alias generator: Added tests confirming alias_generator is supported in Pydantic v2 for SQLModel.
Explicit field alias still takes precedence over the generator for both input and output.
Tests - All passing.
Let me know if this looks ok.
The idea looks good to me!
A couple of more things:
- I think it would be nice to use
aliasas a default value for DB column. It can then be overridden bysa_column=Column("column_name")- We should probably test how it works with alias generators specified in model's config (is it supported? - UPD: YES)
PS: @svlandeg, sorry, I only noticed you self-assigned this PR when I was about to press "Submit review".. Hope you don't mind I submitted this review)
I think it would be better to keep this PR focused on Pydantic v2 alias support, including schema_extra. I suggest opening a follow-up issue to handle using the DB column alias as the default. Let me know your thoughts.
I simplified code a bit - moved alias propagation logic to Field.__init__ so that it would be all in one place.
Also, got rid of deprecation warnings in test.
Could you please let me know when this PR will be merged and released?