diff --git a/openupgradelib/openupgrade.py b/openupgradelib/openupgrade.py index 680e3ae8..45d4df24 100644 --- a/openupgradelib/openupgrade.py +++ b/openupgradelib/openupgrade.py @@ -2883,16 +2883,19 @@ def lift_constraints(cr, table, column, cascade=False): Set cascade=True if other constraints depend on the one you want to delete, which ie is the case for primary keys. If everything went right, the constraints will be recreated.""" + # PostgreSQL 18 stores NOT NULL as a named constraint in pg_constraint, and + # refuses to drop it while the column is still part of a primary key. Sort + # the primary key first so it is dropped before the not null constraint. cr.execute( - "select relname, array_agg(conname) from " - "(select t1.relname, c.conname " + "select relname, array_agg(conname order by (contype = 'p') desc) from " + "(select t1.relname, c.conname, c.contype " "from pg_constraint c " "join pg_attribute a " "on c.confrelid=a.attrelid and a.attnum=any(c.conkey) " "join pg_class t on t.oid=a.attrelid " "join pg_class t1 on t1.oid=c.conrelid " "where t.relname=%(table)s and attname=%(column)s " - "union select t.relname, c.conname " + "union select t.relname, c.conname, c.contype " "from pg_constraint c " "join pg_attribute a " "on c.conrelid=a.attrelid and a.attnum=any(c.conkey) "