Lets say I have the following simplified example:
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)
That should be fine. But now what if I want to add a filter on this query for parent_id, how would I do this? Ideally I would want to do something like
.filter(col.table.parent_id = parent_id)
But col is type sqlalchemy.sql.elements.Label
and col.table is None. What do I do here?