AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Table Editor Drops And Re-Adds Primary Key Due To Order-Sensitive Column Name Comparison

updateTable compares primary key column names in display order with the backend's primary_keys array (which comes back in key order). A rename or an out-of-order composite primary key like (b, a) is falsely detected as a PK change, causing Studio to drop and re-add the constraint in separate transactions. This fails when a foreign key references the primary key, blocking unrelated edits, or silently rebuilds the constraint and reorders columns when no FK exists.

highConfidence 85%Supabase-Studio

Origin Analysis

The PK change detection compares arrays of column names in display order against primary_keys in key order instead of comparing PK membership by column id, leading to false positives for renames and composite key ordering differences.
1. Create a table with a composite primary key (b, a) or a single primary key 'id'. 2. Create another table with a foreign key referencing that primary key. 3. Open the Table Editor and make an unrelated change (e.g., edit description) or rename a non-PK column. 4. Save; Studio erroneously drops the primary key constraint because it thinks the PK changed. 5. The save fails with 'cannot drop constraint ... other objects depend on it' if a FK exists, or silently rebuilds the PK and changes column order to (a, b) if no FK exists.

Fixing Code Block

Edge Case Audit

This fix relies on each primary key column having a stable `id` across renames; if the backend regenerates ids on rename, the comparison could still trigger false positives. Additionally, concurrent schema edits could produce stale `primary_keys` arrays, causing missed or duplicate drops. Rolling back this hotfix after upgrading would reintroduce the order-sensitivity bug for composite keys. Recommended: test with multi-column PKs and FK dependencies, and consider adding optimistic locking or schema version checks for concurrent edits.

Ecosystem Topology