事实:
我的问题是,第一个FROM子句引用了"付款"当没有理由将它包含在发出的SQL中时,它存在于内部查询中。
为什么SQL炼金术会将内部表格中的引用拉入外部表格?
发出的SQL
SELECT users.name , anon_1.count
FROM payment, users
LEFT OUTER JOIN (
SELECT count(payment.creator_id) AS count, payment.creator_id AS creator_id
FROM payment
INNER JOIN accounts ON accounts.id = payment.account_id
INNER JOIN companies ON companies.id = accounts.company_id
WHERE payment.approver_id IS NOT NULL
GROUP BY creator_id
) AS anon_1
ON users.id = anon_1.creator_id
的Python
subquery = self.db.query(*[func.count(Payment.creator_id).label('count'),
Payment.creator_id.label('creator_id')]). \
join(Account, Account.id == Payment.account_id). \
join(Company, Company.id == Account.company_id). \
filter(Payment.approver_id != None). \
group_by('creator_id'). \
subquery()
query = self.db.query(*[func.concat(User.last_name, User.first_name), sub.c.count]). \
outerjoin(subquery, User.id == sub.c.creator_id)
编辑1
我发现我可以从子查询中删除两个连接和过滤器,问题仍然存在。
答案 0 :(得分:0)
我找到了答案。在代码的后面某处,在查询中添加了一个过滤器。