The following code:
Base = declarative_base() engine = create_engine(r\"sqlite:///\" + r\"d:\\foo.db\", listeners=[ForeignKeysListener()])
I cannot explain technically what happens but you can work around this problem by using:
ses.query(FooBar).join(Foobar.bar).join(Bar.foo).filter(Foo.name == "blah")