ownself = db.session.query(User.id.label("user_1_id"), User.id.label("user_id"))
User2 = aliased(User)
FacebookUser2 = aliased(FacebookUser)
friends = (db.session.query(User2.id.label("user_1_id"), User.id.label("user_id")).
join(FacebookUser2, User2.facebook_info == FacebookUser2.id).
join(FriendConnection, FacebookUser2.id == FriendConnection.fb_user_from).
join(FacebookUser, FacebookUser.id == FriendConnection.fb_user_to).
join(User, User.facebook_info == FacebookUser.id)
)
u_all = union_all(ownself, friends)
q = (db.session.query(User).filter(u_all.c.user_id == user.id))
我得到错误“每个派生表必须有自己的别名”与MySQL,我该如何解决这个问题?
答案 0 :(得分:1)
我认为your other question中的评论现在应该涵盖这一点:只需将.alias("alias_name")
添加到相关的(部分)查询中。
在您的代码示例中,将其添加到两个子查询中:
# old:
u_all = union_all(ownself, friends)
# new:
u_all = union_all(ownself.alias("subquery1"), friends.alias("subquery2"))