Hello.
> not sure, is "each.bar" a typo and you meant "_each_bar" ?
No, I mean each.bar.
> or are you trying to fetch a relationship along Foo-> Bar ? if its a
relationship and the linkage is not a simple many-to-one, then it emits SQL.
Yes, I am trying to fetch Foo -> Bar relationship. It is a one-way many-to-one
relationship: Foo has bar_id FK to bar (id).
I still don't understand what I see, but I figured out the cause.
This works:
def test_access_ok(self):
q = self.session.query(Foo, Bar).with_labels()
q = q.filter(Foo.bar_id == Bar.id)
rows = q.all() # IMPORTANT LINE
assert len(rows) > 0
with self.assert_no_sql_while():
for (each_foo, _each_bar) in rows:
each.bar.data
This does not:
def test_access_ko(self):
q = self.session.query(Foo, Bar).with_labels()
q = q.filter(Foo.bar_id == Bar.id)
rows = list(unwrap(q)) # IMPORTANT LINE
assert len(rows) > 0
with self.assert_no_sql_while():
for each in rows:
each.bar.data # <-- FAILS HERE
unwrap is defined as
def unwrap(query):
for each in query:
if isinstance(each, tuple) and len(each) > 0:
yield each[0]
else:
yield each
I wanted to use unwrap to minimize the impact of the queries I have to modify
slightly to overcome SA 0.7 limitation of of_type() in combination with a table
inheritance (we spoke about this in another thread). To make SA fetch everything
I need, the query must return more results (tuples of instances instead of the
instances directly), so I can joinedload from them too. With unwrap in place,
the rest of the query processing can remain intact.
Do you have any idea why the unwrap version does not work?
Thank you,
Ladislav Lenart