Skip to content

Commit 4ee367d

Browse files
committed
[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
1 parent ee04109 commit 4ee367d

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/util/pg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,13 +1287,14 @@ def rename_table(cr, old_table, new_table, remove_constraints=True):
12871287
FROM information_schema.table_constraints
12881288
WHERE table_name = %s
12891289
AND constraint_name !~ '^[0-9_]+_not_null$'
1290+
AND constraint_name != %s
12901291
AND ( constraint_type NOT IN ('PRIMARY KEY', 'FOREIGN KEY')
12911292
-- For long table names the constraint name is shortened by PG to fit 63 chars, in such cases
12921293
-- it's better to drop the constraint, even if it's a foreign key, and let the ORM re-create it.
12931294
OR (constraint_type = 'FOREIGN KEY' AND constraint_name NOT LIKE %s)
12941295
)
12951296
""",
1296-
[new_table, old_table.replace("_", r"\_") + r"\_%"],
1297+
[new_table, '%s_id_not_null' % old_table, old_table.replace("_", r"\_") + r"\_%"],
12971298
)
12981299
for (const,) in cr.fetchall():
12991300
_logger.info("Dropping constraint %s on table %s", const, new_table)

0 commit comments

Comments
 (0)