为SqlAlchemy连接对象添加别名

时间:2016-08-23 16:12:31

标签: python sql sqlalchemy

我正在尝试从子查询中选择一列(下面的代码)。

    import sqlalchemy as sa

    table1 = sa.schema.Table('t1',
                                 db,
                                 autoload=True,
                                 autoload_with=db.engine)
    si = Query().cte()

    pids = sa.select([t1.c.pid], distinct=True).where(t1.c.date > date)

    jt = pids.join(
            si,
            (si.c.pid == t1.c.pid) &
            (si.c.startdate <= date) &
            (si.c.enddate > date))
    return sa.select([si.c.id]).select_from(jt)

但是,我总是收到如下错误:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) subquery in FROM must have an alias
LINE 6: FROM (SELECT DISTINCT t1.pid AS pid 
             ^
HINT:  For example, FROM (SELECT ...) [AS] foo.

我应该如何为子查询添加别名?

0 个答案:

没有答案