Python SQLAlchemy在链接的联合/例外上松开列标签_

Python SQLAlchemy在链接的联合/例外上松开列标签_,python,sqlalchemy,Python,Sqlalchemy,我有一个比较复杂的查询,需要在其中加入子查询。该子查询包含except和union。在原始sql中,它看起来像这样 SELECT ... FROM table t JOIN (SELECT id AS foo_id FROM foo WHERE select_me EXCLUDE SELECT foo_id FROM bar WHERE add_or_remove = 'remove' UNION SELECT foo_id FROM bar WHERE ad

我有一个比较复杂的查询,需要在其中加入子查询。该子查询包含except和union。在原始sql中,它看起来像这样

SELECT ... FROM table t
  JOIN (SELECT id AS foo_id FROM foo WHERE select_me
        EXCLUDE SELECT foo_id FROM bar WHERE add_or_remove = 'remove'
        UNION SELECT foo_id FROM bar WHERE add_or_remove = 'add') subq
    ON t.foo_id = subq.foo_id;
其中,foo和bar表的定义如下:

class Foo(Base):
    __tablename__ = 'foo'

    id = Column(Integer, primary_key=True, autoincrement=True)
    select_me = Column(Boolean)


class Bar(Base):
    __tablename__ = 'bar'

    foo_id = Column(Integer, primary_key=True)
    add_or_remove = Column(Enum('add', 'remove', name='add_or_remove'), primary_key=True)
当我试图在SQLAlchemy中创建这个子查询时,当我添加第二个
union
/
时,它会松开列标签,除了

以下是我要说的:

q = session.query(Foo.id.label('foo_id')).filter(Foo.select_me)
print(q.subquery().c)
打印的
['%(140275696626880 anon)s.foo_id']
仍然包含正确的标签

q = q.union(session.query(Bar.foo_id.label('foo_id')).filter(Bar.add_or_remove == 'add'))
print(q.subquery().c)
q = q.except_(session.query(Bar.foo_id.label('foo_id')).filter(Bar.add_or_remove == 'remove'))
print(q.subquery().c)
打印的
['%(140275696767384 anon)s.foo_id']
仍然包含正确的标签

q = q.union(session.query(Bar.foo_id.label('foo_id')).filter(Bar.add_or_remove == 'add'))
print(q.subquery().c)
q = q.except_(session.query(Bar.foo_id.label('foo_id')).filter(Bar.add_or_remove == 'remove'))
print(q.subquery().c)
打印
['%(140275696769064 anon)s.%(140275696769008 anon)s_foo_id']
现在列标记为自动生成的名称,我无法使用它在
连接中指定条件

现在,我想我可以拿第一列并使用它。但这是一个黑客解决方案,所以我想知道这是SQLAlchemy中的错误还是我做错了什么