Skip to content

Fix Schema Diff omitting DROP CONSTRAINT for primary key and unique constraints when a column is dropped. - #10814

Merged
dpage merged 1 commit into
pgadmin-org:masterfrom
hrkn:fix/schema-diff-drop-constraint-10813
Nov 24, 2026
Merged

dpage merged 1 commit into
pgadmin-org:masterfrom
hrkn:fix/schema-diff-drop-constraint-10813

Conversation

@hrkn

@hrkn hrkn commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #10813

In _get_sql_to_delete_constraints() (web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/utils.py), dropping a primary key or unique constraint was skipped whenever columns_to_be_dropped was non-empty:

del_cols = list(map(lambda x, y: x['column'] in y,
                    c['columns'],
                    data['columns_to_be_dropped']))
if len(del_cols) == 0:

This had three issues:

  1. map() produces a list of booleans (such as [False]), so len(del_cols) == 0 was False whenever any column on the table was dropped—even a column unrelated to the constraint. As a result, DROP CONSTRAINT was omitted while ADD CONSTRAINT was still emitted, failing with multiple primary keys for table ... are not allowed (or leaving stale unique constraints behind).
  2. Two-argument map() zipped c['columns'] and data['columns_to_be_dropped'] positionally and checked x['column'] in y (a substring check against a single column name) rather than set membership against all dropped columns.
  3. INCLUDE columns (c.get('include')), which also cause PostgreSQL to drop the constraint automatically when dropped, were not checked.

Changes

  • Replace the map() / len(del_cols) == 0 check in _get_sql_to_delete_constraints() with a set intersection between data.get('columns_to_be_dropped') and the constraint's key + INCLUDE columns.
  • Add unit tests in test_index_constraint_delete_sql_unit.py covering:
    • Dropping an unrelated column still emits DROP CONSTRAINT (primary key and unique constraint)
    • Dropping a column whose name contains a constraint column as a substring still emits DROP CONSTRAINT
    • Dropping a non-first constraint key column skips DROP CONSTRAINT
    • Dropping an INCLUDE column skips DROP CONSTRAINT
    • No dropped columns emits DROP CONSTRAINT

Verification

  • Verified with pycodestyle --config=.pycodestyle web/
  • Ran the new unit tests and existing index_constraint / schema_diff test suites locally.

Summary by CodeRabbit

  • Bug Fixes
    • Index constraints are now preserved correctly when unrelated columns are dropped, and removed when a dropped column is part of the constraint’s key or included columns.
    • Renamed primary-key and unique constraints now generate SQL that drops the old constraint before adding the renamed one.

…key and unique constraints when a column is dropped. pgadmin-org#10813

Fixes pgadmin-org#10813

In _get_sql_to_delete_constraints(), checking whether a deleted primary
key or unique constraint shared a column with columns_to_be_dropped used
map(lambda x, y: x['column'] in y, c['columns'], data['columns_to_be_dropped'])
and checked len(del_cols) == 0. Because map() returns a list of booleans
whose length is non-zero whenever any column is dropped, DROP CONSTRAINT
was omitted even when an unrelated column was dropped. In addition,
two-argument map() paired lists positionally and performed substring
matching, and INCLUDE columns were not checked.

Check set intersection between the dropped columns and the constraint's
key and INCLUDE columns instead, and add unit tests covering these cases.
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The index-constraint deletion path now checks dropped columns against both key and INCLUDE columns. Unit tests cover when deletion SQL is generated and verify DROP-before-ADD ordering for recreated constraints.

Changes

Index constraint deletion

Layer / File(s) Summary
Constraint deletion and SQL validation
web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/utils.py, web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/tests/test_index_constraint_delete_sql_unit.py
The deletion path checks dropped columns against constraint key and INCLUDE columns before generating DROP CONSTRAINT SQL. Tests cover matching and unrelated columns, and verify that DROP precedes ADD for recreated constraints.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: dpage

Merge Risk: Minimal · up to 31645

Schema Diff now emits DROP CONSTRAINT for primary key and unique constraints unless a column in the constraint is itself dropped. The change is small, localized, and covered by unit tests, so merge risk looks minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check Passed Issue #10813 requires that Schema Diff emit DROP CONSTRAINT for a primary key or unique constraint that is being recreated, even when an unrelated column is also dropped. At the reviewed head, _get_sq…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes utils.py in _get_sql_to_delete_constraints() only, and per the summary adds unit tests in test_index_constraint_delete_sql_unit.py. The utils.py change…
Title check Passed The title names the specific bug fixed: Schema Diff omitting DROP CONSTRAINT for primary key and unique constraints when a column is dropped. The changeset replaces the per-column check in the index c…
Full details: Linked Issues check

Explanation

Issue #10813 requires that Schema Diff emit DROP CONSTRAINT for a primary key or unique constraint that is being recreated, even when an unrelated column is also dropped. At the reviewed head, _get_sql_to_delete_constraints() in index_constraint/utils.py builds constraint_cols from each deleted constraint's 'columns' (the 'column' key) and 'include' lists, then intersects them with set(data.get('columns_to_be_dropped') or []). The DROP is skipped only when that intersection is non-empty. For the issue example (key columns a, b; dropped column c), the intersection is empty and DROP CONSTRAINT is emitted before ADD CONSTRAINT. The map()/len() check that caused the bug is removed. Key-column drops and INCLUDE-column drops still skip DROP, which matches PostgreSQL's automatic constraint removal. 'include' values are plain column names from get_constraint_include.sql (colname), so the set comparison is valid. The PR summary reports new unit tests for these cases in test_index_constraint_delete_sql_unit.py. I did not run them; the test content is supported only by the summary. No coding requirement of #10813 remains unmet in the source.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes utils.py in _get_sql_to_delete_constraints() only, and per the summary adds unit tests in test_index_constraint_delete_sql_unit.py. The utils.py change is the fix itself. The tests support the fix and do not add unrelated behavior. The related change to the recreation path is covered by the same issue. No unrelated changes were identified.

Full details: Title check

Explanation

The title names the specific bug fixed: Schema Diff omitting DROP CONSTRAINT for primary key and unique constraints when a column is dropped. The changeset replaces the per-column check in the index constraint delete logic with a set intersection against key and INCLUDE columns, and adds tests for that behavior. The title matches the main change. It is specific and reads clearly, though at 105 characters it is somewhat long.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between ccabf7e and 3164519.

Files selected for processing: 2

  • web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/tests/test_index_constraint_delete_sql_unit.py
  • web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/utils.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@hrkn hrkn changed the title Fix Schema Diff omitting DROP CONSTRAINT for primary key and unique constraints when a column is dropped. #10813 Fix Schema Diff omitting DROP CONSTRAINT for primary key and unique constraints when a column is dropped. Nov 23, 2026

@dpage dpage left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for the clear write-up in #10813.

The old check in _get_sql_to_delete_constraints() was broken in exactly the way you describe: map() always produced a non-empty list once any column was dropped, so DROP CONSTRAINT was skipped for every deleted primary key or unique constraint, whether or not it touched the dropped column. Replacing it with a set intersection against the key and INCLUDE columns is the right fix, and it also corrects the same misbehaviour in the table properties dialog, which goes through the same code path. The column names compare like for like (unquote_ident() on the key columns, raw attname for INCLUDE columns, raw names for the dropped columns), and since delete.sql uses DROP CONSTRAINT IF EXISTS and the column drops are emitted first, the skip is safe either way.

I ran the index_constraint test package against PostgreSQL 18 (58 passed) and confirmed that five of the new scenarios fail against the old code, so the tests do cover the bug. pycodestyle is clean.

Suggestion

  • test_index_constraint_delete_sql_unit.py: the first two scenarios of TestIndexConstraintDeleteSqlUnit differ only in the constraint name, and _get_sql_to_delete_constraints() never sees the constraint type, so the unique constraint case duplicates the primary key one. The type-specific path is already covered by TestIndexConstraintDropAndAddSqlUnit, so you could drop the second scenario. Not a blocker.

  • Correctness

  • Security

  • Error handling

  • Tests

  • Documentation (none needed)

@dpage dpage self-assigned this Nov 24, 2026
@dpage
dpage merged commit 3711453 into pgadmin-org:master Nov 24, 2026
32 checks passed
@hrkn
hrkn deleted the fix/schema-diff-drop-constraint-10813 branch November 24, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Schema Diff omits DROP CONSTRAINT for primary key / unique constraints when any column is also dropped

2 participants