Repository navigation
Description
Summary
When an index rename is required and the live index is backed by a UNIQUE constraint, meta migrate --apply emits DROP INDEX and Postgres refuses it. The migration cannot be applied at all, and because meta migrate is all-or-nothing per invocation, this blocks every other pending change in the same repo — including unrelated CREATE TABLEs.
Found while adopting MetaObjects in a real project (an adopter with 5 tables, previously hand-written in Drizzle).
Reproduction
Live schema created by Drizzle, whose unique indexes are UNIQUE constraints:
conname | contype | backing_index
work_item_message_id_unique | u | work_item_message_id_unique
Metadata declares identity.secondary named byMessageId on the same column, so migrate correctly detects a rename is needed:
changes[4]{kind,count}:
add-index,2
drop-index,2
add-fk,2
drop-fk,2
Applying fails:
meta: migrate: apply failed: cannot drop index work_item_message_id_unique
because constraint work_item_message_id_unique on table work_item requires it
The emitted SQL is:
DROP INDEX "work_item_message_id_unique";Expected
For an index that backs a constraint, Postgres requires the constraint be dropped (or renamed) instead:
ALTER TABLE "work_item" DROP CONSTRAINT "work_item_message_id_unique";
-- or, for a pure rename, the cheaper and non-destructive:
ALTER TABLE "work_item" RENAME CONSTRAINT "work_item_message_id_unique" TO "byMessageId";ALTER TABLE ... RENAME CONSTRAINT renames both the constraint and its backing index in one statement, so a detected index rename need not be a drop/create pair at all.
Impact
- The apply is transactional, so nothing is left half-applied — good. But the migration simply cannot proceed.
- Because migrate is all-or-nothing, this also blocks unrelated changes: in our case a new
CREATE TABLEcould not be applied while these four cosmetic renames were pending. - Adopters coming from Drizzle will hit this on essentially every unique index, since Drizzle's
unique()produces constraints rather than bare indexes.
Workaround (not applied here)
Manually running ALTER TABLE ... RENAME CONSTRAINT aligns the live DB to the metadata and clears the diff. We deliberately have not done this — the project's own rule is that schema changes flow through metadata → meta gen → meta migrate, and we would rather wait for a fix than hand-edit a live database a second time.
Related
Same adopter also hit, and documented, the absence of an add/drop-primary-key change kind (migrate cannot express a primary-key change) when a live table has no PK but the metadata declares one. Separate issue, mentioning only because both surfaced in the same adoption pass.
Versions: @metaobjectsdev/[email protected], dialect postgres, PostgreSQL 16.
Activity
fix(migrate-ts): drop a constraint-backed index as a constraint (#285)
dmealing commentedon Aug 9, 2026
Fixed on
maininf6a4ce6f— ships in the next release.Root cause is exactly as you diagnosed. Postgres introspection never learned whether an index was constraint-backed, so the emitter had no way to know
DROP INDEXwould be refused. It now readspg_constraint.conindid— the catalog's own back-pointer, so it's exact rather than a name heuristic — and marks the descriptorconstraint: "unique" | "primary" | "exclude". The emitter drops the constraint instead.The down migration also had to change: it was recreating a bare unique index, which would have left the database in a different shape than it started and made a second rollback fail to find the constraint it expected. It now re-adds a constraint.
The marker is introspection-only — never authored in metadata, never on the expected side, and deliberately not compared by
indexEquals, since a unique index and a unique constraint over the same columns are the same model-level thing and must not read as drift.Gated against a real Postgres 16, not by inspecting emitted SQL — emitted-SQL inspection is precisely what missed this, because the SQL looked perfectly reasonable. The new gate builds the live shape the way Drizzle does, applies, re-introspects, requires the re-diff to be empty, and then proves uniqueness is still enforced — a migration that dropped the constraint and never re-added an equivalent would also "converge". Verified to fail with the fix reverted.
Thanks for the report — the catalog dump and the verbatim error made this quick, and your note that Drizzle's
unique()produces constraints is what made clear this is a broad adopter path rather than an edge case.Not addressed, deliberately: your suggestion that
ALTER TABLE … RENAME CONSTRAINTwould turn a pure rename into one non-destructive statement is right, and better than a drop/add pair — but it needs the diff to model index renames, which it doesn't today. The drop/add is correct and converges; it just rebuilds the index. Worth doing separately.Leaving this open until it's actually published so you're not waiting on a version that isn't there yet.
dmealing commentedon Aug 9, 2026
Fixed and released in 0.21.4 (npm
@metaobjectsdev/[email protected]).A constraint-backed index is now dropped as a constraint, not as an index, so the emitted migration applies cleanly.
Gated by an emit → apply-to-a-real-engine → introspect → re-diff-must-be-empty round-trip, per the project's standing migrate rule that proving SQL correct is not the same as proving it appliable.
See
CHANGELOG.md[0.21.4].docs: propagate 0.21.5 / 7.21.5 across README, llms.txt, port docs an…
feat(metadata): add field.duration, validator.exactlyOne and schema c…