Ich schrieb eine Abfrage für mysql, die erreicht, was ich wollte. Es ist ein bisschen wie folgt strukturiert:Wie verhindert man, dass geschachtelte Abfragen in sqlalchemy erneut eine Tabelle auswählen?
select * from table_a where exists(
select * from table_b where table_a.x = table_b.x and exists(
select * from table_c where table_a.y = table_c.y and table_b.z = table_c.z
)
)
ich die Abfrage übersetzt sqlalchemy und das Ergebnis wie folgt strukturiert:
session.query(table_a).filter(
session.query(table_b).filter(table_a.x == table_b.x).filter(
session.query(table_c).filter(table_a.y == table_c.y).filter(table_b.x == table_c.z).exists()
).exists()
)
, die eine Abfrage wie folgt erzeugt:
select * from table_a where exists(
select * from table_b where table_a.x = table_b.x and exists(
select * from table_c, table_a where table_a.y = table_c.y and table_b.z = table_c.z
)
)
Hinweis die Neuauswahl von table_a
in der innersten Abfrage - was die beabsichtigte Funktionalität bricht.
Wie kann ich verhindern, dass sqlalchemy die Tabelle in einer verschachtelten Abfrage erneut auswählt?