I want to add branch_id variable in sql query. How can I use?When I use below code,I got psycopg2.ProgrammingError: column reference "branch_id" is ambiguous.
branch_id = self.env.user.branch_id.id
query = '''
SELECT DISTINCT l.partner_id, res_partner.name AS name, UPPER(res_partner.name) AS UPNAME, CASE WHEN prop.value_text IS NULL THEN 'normal' ELSE prop.value_text END AS trust
FROM account_move_line AS l
LEFT JOIN res_partner ON l.partner_id = res_partner.id
LEFT JOIN ir_property prop ON (prop.res_id = 'res.partner,'||res_partner.id AND prop.name='trust' AND prop.company_id=%s),
account_account, account_move am
WHERE (l.account_id = account_account.id)
AND (l.move_id = am.id)
AND (am.state IN %s)
AND (account_account.internal_type IN %s)
AND (
l.reconciled IS NOT TRUE
OR l.id IN(
SELECT credit_move_id FROM account_partial_reconcile where max_date > %s
UNION ALL
SELECT debit_move_id FROM account_partial_reconcile where max_date > %s
)
)
''' + partner_clause + '''
AND (l.date <= %s)
AND (l.branch_id = branch_id)
AND l.company_id IN %s
ORDER BY UPPER(res_partner.name)'''
arg_list = (self.env.company.id,) + arg_list
cr.execute(query, arg_list)
(l.branch_id = branch_id)here thebranch_idis not considered as a paramater, but as a database columns (that is not qualified, which leads to the problem). You must add thebranch_idto thearg_list(in correct order) and mark it in the query as a parameter with the%sign