python sqlalchemy 联合查询,'Query' 对象没有属性'spends'

python sqlalchemy union query,'Query' object has no attribute 'spends'

#app adset
app_list = db.session.query(
    AdsetAppReport.adset_id.label("adset_id"),
    func.sum(AdsetAppReport.spend).label("spends"),
    ).group_by(AdsetAppReport.adset_id)

#web adset
web_list = db.session.query(
    AdsetReport.adset_id.label("adset_id"),
    func.sum(AdsetReport.spend).label("spends"),
    ).group_by(AdsetReport.adset_id)

#union two tables
both_list = app_list.union(web_list)

#Query
adset_list = db.session.query(
    Adset.adset_id,
    Adset.user_os,
    both_list.spends,
    ).filter(both_list.adset_id == Adset.adset_id) \
     .all()

我想将两个table合并为both_list,然后在临时table中进行adset inner join。 当我 运行 代码时,它向我显示 AttributeError。 AttributeError: both_list 'Query' 对象没有属性 'spends'

我无法检查这是否有效,但创建子查询可能会解决它。此外,我明确表示了连接。

#union two tables
both_list = app_list.union(web_list).subquery()

#Query
adset_list = db.session.query(
        Adset.adset_id,
        Adset.user_os,
        both_list.c.spends,
    ).\
    join(both_list, both_list.c.adset_id == Adset.adset_id).\
    all()