Skip to content

✨ Add support for SQLAlchemy polymorphic models #1226

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 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6d93a46
support sqlalchemy polymorphic
Nov 26, 2024
589237b
improve docs
Nov 26, 2024
4071b0f
fix polymorphic_on check
Nov 26, 2024
48f2a88
fix polymorphic_on check
Nov 26, 2024
e6ad74d
fix lint
Nov 26, 2024
277953a
fix pydantic v1 support
Nov 26, 2024
4aade03
fix type hint for <3.10
Nov 26, 2024
a3044bb
add needs_pydanticv2 mark to test
Nov 26, 2024
015601c
improve code structure
Dec 3, 2024
66c1d93
lint
Dec 3, 2024
0efd1bf
remove effort of pydantic v1
Dec 3, 2024
7173b44
Merge branch 'main' into sqlalchemy_polymorphic_support
PaleNeutron Dec 10, 2024
84d739e
Update sqlmodel/_compat.py
PaleNeutron Dec 12, 2024
dbd0101
fix default value is InstrumentedAttribute in inherit
Feb 5, 2025
88670a5
Merge branch 'sqlalchemy_polymorphic_support' of https://github.com/P…
Feb 5, 2025
b1ed8c3
fix inherit order
Feb 5, 2025
5d1bf5c
support python < 3.9
Feb 5, 2025
ccbb92a
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] Feb 5, 2025
d0d0288
skip polymorphic in pydantic v1
Feb 5, 2025
525373d
Merge branch 'sqlalchemy_polymorphic_support' of https://github.com/P…
Feb 5, 2025
ab965f0
Merge branch 'main' into sqlalchemy_polymorphic_support
PaleNeutron Feb 5, 2025
68963e7
Merge branch 'main' into sqlalchemy_polymorphic_support
svlandeg Feb 20, 2025
95c6a1e
Merge branch 'main' into sqlalchemy_polymorphic_support
svlandeg Feb 24, 2025
c1dff79
disable pydantic warning during polymorphic
Mar 12, 2025
7d333fe
fix relationship problem of parent class during polymorphic inherit
May 15, 2025
32ebd6f
avoid add ClassVar multiple times
May 15, 2025
64f774f
Add support for polymorphic_abstract
May 27, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve code structure
  • Loading branch information
John Lyu committed Dec 3, 2024
commit 015601cd5bd4d805ab8474429d8de7310c883060
42 changes: 26 additions & 16 deletions sqlmodel/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ def _is_union_type(t: Any) -> bool:
finish_init: ContextVar[bool] = ContextVar("finish_init", default=True)


def set_polymorphic_default_value(self_instance, values):
"""By defalut, when init a model, pydantic will set the polymorphic_on
value to field default value. But when inherit a model, the polymorphic_on
should be set to polymorphic_identity value by default."""
cls = type(self_instance)
mapper = inspect(cls)
if isinstance(mapper, Mapper):
polymorphic_on = mapper.polymorphic_on
if polymorphic_on is not None:
polymorphic_property = mapper.get_property_by_column(polymorphic_on)
field_info = get_model_fields(cls).get(polymorphic_property.key)
if field_info:
v = values.get(polymorphic_property.key)
# if model is inherited or polymorphic_on is not explicitly set
# set the polymorphic_on by default
if mapper.inherits or v is None:
setattr(
self_instance,
polymorphic_property.key,
mapper.polymorphic_identity,
)


@contextmanager
def partial_init() -> Generator[None, None, None]:
token = finish_init.set(False)
Expand Down Expand Up @@ -293,22 +316,7 @@ def sqlmodel_table_construct(
setattr(self_instance, key, value)
# End SQLModel override
# Override polymorphic_on default value
mapper = inspect(cls)
if isinstance(mapper, Mapper):
polymorphic_on = mapper.polymorphic_on
if polymorphic_on is not None:
polymorphic_property = mapper.get_property_by_column(polymorphic_on)
field_info = cls.model_fields.get(polymorphic_property.key)
if field_info:
v = values.get(polymorphic_property.key)
# if model is inherited or polymorphic_on is not explicitly set
# set the polymorphic_on by default
if mapper.inherits or v is None:
setattr(
self_instance,
polymorphic_property.key,
mapper.polymorphic_identity,
)
set_polymorphic_default_value(self_instance, values)
return self_instance

def sqlmodel_validate(
Expand Down Expand Up @@ -592,3 +600,5 @@ def sqlmodel_init(*, self: "SQLModel", data: Dict[str, Any]) -> None:
for key in non_pydantic_keys:
if key in self.__sqlmodel_relationships__:
setattr(self, key, data[key])
# Override polymorphic_on default value
set_polymorphic_default_value(self, values)
4 changes: 2 additions & 2 deletions tests/test_polymorphic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DarkHero(Hero):


@needs_pydanticv2
def test_polymorphic_joined_table_sm_field(clear_sqlmodel) -> None:
def test_polymorphic_joined_table_with_sqlmodel_field(clear_sqlmodel) -> None:
class Hero(SQLModel, table=True):
__tablename__ = "hero"
id: Optional[int] = Field(default=None, primary_key=True)
Expand Down Expand Up @@ -123,7 +123,7 @@ class DarkHero(Hero):
with Session(engine) as db:
hero = Hero()
db.add(hero)
dark_hero = DarkHero()
dark_hero = DarkHero(dark_power="pokey")
db.add(dark_hero)
db.commit()
statement = select(DarkHero)
Expand Down
Loading