I would like to get instances (rows) of table foo
that do not have a reference in bar
Table foo
:
+----+-----+
| id | baz |
+----+-----+
| 1 | 23 |
| 2 | 56 |
| 3 | 45 |
| 4 | 78 |
+----+-----+
table bar
:
+-----+--------+-----+
| id | foo_id | zab |
+-----+--------+-----+
| 7 | 2 | s1 |
| 8 | 4 | s2 |
+-----+--------+-----+
The result of my query should be instances of foo
as below:
+----+-----+
| id | baz |
+----+-----+
| 1 | 23 |
| 3 | 45 |
+----+-----+
Using the SQLAlchemy ORM, I have tried join
and outerjoin
, but the solution is still escaping me. Something tells me that the solution is easy and right in front of my face...
q = db.session.query(Foo).join(Baz, Baz.foo_id == Foo.id)
q = db.session.query(Foo).outerjoin(Baz, Baz.foo_id == Foo.id)