Skip to content

Preserve CHECK constraints when transforming a table#766

Open
apoorva-01 wants to merge 1 commit into
simonw:mainfrom
apoorva-01:fix/762-transform-preserve-check-constraints
Open

Preserve CHECK constraints when transforming a table#766
apoorva-01 wants to merge 1 commit into
simonw:mainfrom
apoorva-01:fix/762-transform-preserve-check-constraints

Conversation

@apoorva-01

@apoorva-01 apoorva-01 commented Jul 1, 2026

Copy link
Copy Markdown

.transform() quietly drops a table's CHECK constraints. They only live in the stored CREATE TABLE SQL, and transform rebuilds from PRAGMA metadata, which doesn't expose them.

This pulls the CHECK expressions out of the schema and re-adds them. A rename rewrites the columns a constraint references, and dropping a referenced column drops its constraint, so transform never emits a table that won't build. Column-level checks come back as table-level (identical in SQLite). It uses a small quote/paren-aware tokenizer, so no new dependency; the one edge it can't see is a column whose name also appears as a function call (length alongside length(x)), left as a documented limitation.

I scoped this to CHECK constraints. Comments and column-level UNIQUE are follow-ups (comment placement gets ambiguous on rename/reorder; UNIQUE is the separate auto-index case), happy to take either. Tests cover round-trip, rename, drop and parser edges and fail without the fix. Didn't touch the changelog since you compile it at release.

Closes #762


📚 Documentation preview 📚: https://sqlite-utils--766.org.readthedocs.build/en/766/

.transform() rebuilds from PRAGMA metadata, which doesn't expose CHECK
constraints, so they were silently dropped. Pull them from the schema
and re-add them to the rebuilt table.
@apoorva-01 apoorva-01 force-pushed the fix/762-transform-preserve-check-constraints branch from 7a46fa5 to 265f850 Compare July 2, 2026 02:50
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.

.transform() drops check constraints and comments

2 participants