sqlalchemy select *具有别名的子查询不起作用

时间:2019-06-20 01:16:21

标签: python postgresql sqlalchemy

我正在动态生成sqlalchemy查询:

def get_objects(
    self, from_table, filters, select_=[], group_=[], order_=[], count=False
):
    if group_ and set(select_) != set(group_):
        raise ValueError(
            'Grouped columns must be the same as the selected columns'
        )

    if count and not group_:
        raise ValueError(
            'Count function is only allowed in combination with a group'
        )

    if select_:
        select_cols = [column(s) for s in select_]
        if count:
            select_cols += [func.count()]
    else:
        select_cols = ['*']

    selelct_stmt = select(
        from_obj=from_table, columns=select_cols, order_by=order_, group_by=group_
    ).alias('tmp')

    query = self._session.query(selelct_stmt)

    #  some more stuff being done with the query...

    return query.all()

按如下所示调用函数时

get_objects(SomeTable, None)

它返回错误

sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'tmp.*'"

我调试了发送的实际选择

SELECT tmp.* 
FROM (SELECT * 
FROM ruletemplate) AS tmp

尽管在PostgresDB中执行此操作也可以正常工作...

0 个答案:

没有答案