from sqlalchemy import select
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "user_account"
id: Mapped[int] = mapped_column(primary_key=True)
address_id: Mapped[int] = mapped_column(ForeignKey("address.id"))
model_type: Mapped[str] = mapped_column(String(50))
__mapper_args__ = {"polymorphic_identity": "base", "polymorphic_on": "model_type"}
class Admin(User):
__mapper_args__ = {"polymorphic_identity": "admin"}
class Address(Base):
__tablename__ = "address"
id: Mapped[int] = mapped_column(primary_key=True)
print(select(Address.id).select_from(Admin).join(Address))