gpt4 book ai didi

python - 如何在sql查询odoo中使用变量

转载 作者:行者123 更新时间:2023-12-04 08:56:21 25 4
gpt4 key购买 nike

我想在 sql 查询中添加 branch_id 变量。我该如何使用?当我使用下面的代码时,我得到了 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)

最佳答案

我更喜欢使用名称进行查询的变量替换,这将使这更容易理解。例如:

    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=%(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 %(state)s)
AND (account_account.internal_type IN %(internal_types)s
AND (
l.reconciled IS NOT TRUE
OR l.id IN(
SELECT credit_move_id FROM account_partial_reconcile where max_date > %(max_date)s
UNION ALL
SELECT debit_move_id FROM account_partial_reconcile where max_date > %(max_date)s
)
)
''' + partner_clause + '''
AND (l.date <= %(date)s)
AND (l.branch_id = %(branch_id)s)
AND l.company_id IN %(company_id)s
ORDER BY UPPER(res_partner.name)'''
args = {
'company_id': ...,
'internal_types': ...,
'max_date': ...,
'date': ...,
'branch_id': ...,
}
cr.execute(query, args)

关于python - 如何在sql查询odoo中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63810286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com