Код: Выделить всё
from sqlalchemy.orm import DeclarativeBase, Mapped
class TableA(DeclarativeBase):
col_name = Mapped[str | None]
parent_id = Mapped[int]
class TableB(DeclarativeBase):
col_name = Mapped[str | None]
parent_id = Mapped[int]
class TableC(DeclarativeBase):
col_name = Mapped[str | None]
parent_id = Mapped[int]
MY_MAPPINGS = {
"a__col_name" : func.coalesce(TableA.col_name, "").label("some_col_name"),
"b__col_name" : func.coalesce(TableB.col_name, "").label("another__col_name"),
"c__col_name" : func.coalesce(TableC.col_name, "").label("third__col_name"),
}
def get_query(mapping: str, parent_id: int):
col = MY_MAPPINGS[mapping]
return select(col)
Код: Выделить всё
.filter(col.table.parent_id = parent_id)
Подробнее здесь: https://stackoverflow.com/questions/784 ... sqlalchemy