Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) "
Expand Down
Loading