[FIX] merge_models: psycopg2 param style + double-AND typo (marketing_card UPDATE)#450
Merged
MiquelRForgeFlow merged 1 commit intoMay 21, 2026
Conversation
…ng_card UPDATE
The card_campaign cross-reference UPDATE inside merge_models() (active when
marketing_card is installed) had two bugs:
1. Used `{old_model}` / `{new_model}` as sql.SQL().format() placeholders but
passed them via the psycopg2 params dict — raising
`KeyError: 'old_model'` from psycopg2/sql.py at runtime.
2. Double `AND` in WHERE clause:
`AND mt.{ref_field} = c.res_id AND AND c.campaign_id = cc.id`
Both fixed: switch to `%(old_model)s` / `%(new_model)s` (matching the params
dict) and remove the redundant AND.
Triggered in practice by any model-merge migration when marketing_card is
installed (e.g., OCA/OpenUpgrade hr_recruitment hr.candidate -> hr.applicant
merge on 18.0 -> 19.0).
MiquelRForgeFlow
approved these changes
May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
The
merge_models()helper'scard_campaigncross-reference UPDATE (active whenmarketing_cardis installed) has two issues atopenupgradelib/openupgrade.py:1210-1226:{old_model}/{new_model}are written assql.SQL().format()placeholders, but they're passed via the psycopg2 query-params dict instead. Result at runtime:KeyError: 'old_model'frompsycopg2/sql.py:254insql.SQL.format().AND mt.{ref_field} = c.res_id AND AND c.campaign_id = cc.id— redundantAND(SQL syntax error if the first bug ever got past the formatter).Source
Introduced by #436 (
[FIX] merge_models: check table columns exist, merged 2026-02-09 by @MiquelRForgeFlow, approved by @pedrobaeza; @hbrunn requested changes on an unrelatedbase_automationconcern that was resolved). Commitf7f5eab7—[FIX] merge_models: correctly handle marketing_card. ~3.4 months old at time of report.Why nobody caught it: the
if is_module_installed(cr, 'marketing_card')gate means the buggy code path only fires when both marketing_card is installed and a model-merge migration runs. The OCA test seed at the time apparently didn't combine those.Reproduction
Real-world hit:
OCA/OpenUpgradePR #5651 — whenmarketing_cardis added toMODULES_NEW(so the seed has it installed),hr_recruitment/19.0.1.1/pre-migration.py:14's call toopenupgrade.merge_models(env.cr, "hr.candidate", "hr.applicant", "candidate_id")triggers this code path and crashes withKeyError: 'old_model'.Minimal psycopg2 repro (verified locally in the lab):
Fix
Switch the two value placeholders to psycopg2 param style (
%(old_model)s/%(new_model)s, matching the dict already being passed) and drop the redundantAND:Verification
sql.SQL().format()→KeyError: 'old_model'%(old_model)s/%(new_model)sremain as query params forcursor.execute,ref_fieldbecomes a quoted identifier