From 34c3c1f7a8e1f56b7f414ea68183e59d4cc9ffd1 Mon Sep 17 00:00:00 2001 From: Alvaro Fuentes Date: Fri, 29 Aug 2025 09:53:05 +0200 Subject: [PATCH] [FIX] util/pg: drop indexes when removing constraints If a constraint with the same name was found. Attempt as well to drop any existing index with the same name. Otherwise we may leave behind constraints implemented as unique indexes. See odoo/upgrade#8318 --- src/util/pg.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/util/pg.py b/src/util/pg.py index cd48a95c4..901fcb5e9 100644 --- a/src/util/pg.py +++ b/src/util/pg.py @@ -795,8 +795,8 @@ def remove_constraint(cr, table, name, cascade=False, warn=True): """ _validate_table(table) log = _logger.warning if warn else _logger.info - cascade = "CASCADE" if cascade else "" - cr.execute('ALTER TABLE "{}" DROP CONSTRAINT IF EXISTS "{}" {}'.format(table, name, cascade)) + cascade = SQLStr("CASCADE" if cascade else "") + cr.execute(format_query(cr, "ALTER TABLE {} DROP CONSTRAINT IF EXISTS {} {}", table, name, cascade)) # Exceptionally remove Odoo records, even if we are in PG land on this file. This is somehow # valid because ir.model.constraint are ORM low-level objects that relate directly to table # constraints. @@ -804,6 +804,8 @@ def remove_constraint(cr, table, name, cascade=False, warn=True): if cr.rowcount: ids = tuple(c for (c,) in cr.fetchall()) cr.execute("DELETE FROM ir_model_data WHERE model = 'ir.model.constraint' AND res_id IN %s", [ids]) + # The constraint was found remove any index with same name + cr.execute(format_query(cr, "DROP INDEX IF EXISTS {}", name)) return True if name.startswith(table + "_"): log("%r not found in ir_model_constraint, table=%r", name, table)