From 4ee367dbdff0063a933ade9d8c07afae1a5ee735 Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Thu, 9 Oct 2025 11:27:13 +0200 Subject: [PATCH] [FIX] util: fix constraint removal in postgres 18 Since postgres 18 the not null on a field is an explicit constraint named tablename_colname_not_null. When trying to drop all constraint from a model, an error occurs when trying to drop the id primary key not null constraint. ALTER TABLE "mail_presence" DROP CONSTRAINT IF EXISTS "bus_presence_id_not_null" ERROR: column "id" is in a primary key This commit filters the constraints to remove the id not null one. We may want to make this filter generic on all not null constraint --- src/util/pg.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/util/pg.py b/src/util/pg.py index 3839cf7f3..a86918640 100644 --- a/src/util/pg.py +++ b/src/util/pg.py @@ -1287,13 +1287,14 @@ def rename_table(cr, old_table, new_table, remove_constraints=True): FROM information_schema.table_constraints WHERE table_name = %s AND constraint_name !~ '^[0-9_]+_not_null$' + AND constraint_name != %s AND ( constraint_type NOT IN ('PRIMARY KEY', 'FOREIGN KEY') -- For long table names the constraint name is shortened by PG to fit 63 chars, in such cases -- it's better to drop the constraint, even if it's a foreign key, and let the ORM re-create it. OR (constraint_type = 'FOREIGN KEY' AND constraint_name NOT LIKE %s) ) """, - [new_table, old_table.replace("_", r"\_") + r"\_%"], + [new_table, '%s_id_not_null' % old_table, old_table.replace("_", r"\_") + r"\_%"], ) for (const,) in cr.fetchall(): _logger.info("Dropping constraint %s on table %s", const, new_table)