Discussed in #9624
this mapping is impossible:
from __future__ import annotations
from sqlalchemy.orm import column_property
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import MappedAsDataclass
from sqlalchemy.orm import query_expression
class Base(MappedAsDataclass, DeclarativeBase):
pass
class A(Base):
__tablename__ = "a"
id: Mapped[int] = mapped_column(primary_key=True, init=False)
data: Mapped[str] = mapped_column()
foo: Mapped[str] = query_expression()
bar: Mapped[str] = column_property(data + "fasdf")
a1 = A(data='mydata')
query_expression and column_property at runtime are pulled in, with no way to pass init=False, so that's a runtime change. then also these two constructs are erroneously in the dataclass_transform decorator so they don't type-check correctly either. major issue has to be fixed ASAP
the workaround is as follows
class A(Base):
__tablename__ = "a"
id: Mapped[int] = mapped_column(primary_key=True, init=False)
data: Mapped[str] = mapped_column()
if TYPE_CHECKING:
foo: Mapped[str] = mapped_column(init=False)
bar: Mapped[str] = mapped_column(init=False)
else:
foo = query_expression()
bar = column_property(data + "fasdf")
Discussed in #9624
this mapping is impossible:
query_expression and column_property at runtime are pulled in, with no way to pass init=False, so that's a runtime change. then also these two constructs are erroneously in the
dataclass_transformdecorator so they don't type-check correctly either. major issue has to be fixed ASAPthe workaround is as follows