sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

🐛 Fix `alias` support for Pydantic v2

Open ravishan16 opened this issue 3 months ago • 6 comments

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:

  1. Enhanced Field function:

    • Added validation_alias and serialization_alias parameters
    • Implemented version-specific parameter handling
  2. Automatic alias propagation:

    • ~~Modified post_init_field_info in _compat.py to automatically set validation_alias and serialization_alias from alias when not explicitly provided (Pydantic v2 only)~~ UPD YuriiMotov: updated it to just using alias value for validation_alias and serialization_alias if they are not specified directly
  3. 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
  4. 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

ravishan16 avatar Sep 25 '25 22:09 ravishan16

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!

svlandeg avatar Oct 01 '25 09:10 svlandeg

@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.

ravishan16 avatar Oct 04 '25 20:10 ravishan16

@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.

ravishan16 avatar Oct 09 '25 01:10 ravishan16

The idea looks good to me!

A couple of more things:

  • I think it would be nice to use alias as a default value for DB column. It can then be overridden by sa_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.

ravishan16 avatar Oct 09 '25 01:10 ravishan16

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.

YuriiMotov avatar Oct 09 '25 16:10 YuriiMotov

Could you please let me know when this PR will be merged and released?

xdewx avatar Nov 13 '25 01:11 xdewx