Schema and migration contracts
What you need to know
A schema defines what data the database accepts. A migration is a versioned change to that schema, and must preserve valid data across application releases.
By the end, you can: explain constraints and migration ordering; define rollback limitations.
Schema with ownership and inventory constraints
-- SQLite fixture, foreign keys enabled before BEGINPRAGMA foreign_keys = ON;CREATE TABLE readers(id TEXT PRIMARY KEY NOT NULL);CREATE TABLE books( id TEXT PRIMARY KEY NOT NULL, owner_id TEXT NOT NULL REFERENCES readers(id), title TEXT NOT NULL CHECK(length(trim(title)) BETWEEN 3 AND 60), quantity INTEGER NOT NULL CHECK(quantity BETWEEN 0 AND 100)) STRICT;CREATE INDEX books_owner ON books(owner_id);Expected behavior: Valid owned books can be inserted; an unknown owner, invalid title or negative quantity is rejected. STRICT is a SQLite-specific table option.
How it works
Use primary keys for identity, foreign keys for relationships, NOT NULL for required values, UNIQUE for uniqueness and CHECK for business conditions that can be enforced at the database. Validation in a Server Action gives useful feedback but cannot replace constraints when requests race. In SQLite, explicitly enable and verify foreign-key enforcement per connection before starting a transaction. Run PRAGMA foreign_key_check after a disposable migration; an empty result indicates no detected foreign-key violations. Deferred foreign keys can permit an intermediate inconsistency until COMMIT; this does not remove the final constraint. Indexes improve particular access patterns, but add write and storage costs. For a new required field in a live deployment, first add a compatible column, backfill in a bounded verified migration, update readers/writers, then enforce the final invariant; old code may still be running during rollout. A rollback of application code does not automatically undo a schema or recover deleted data. Review migration SQL even when generated by an ORM. Use only disposable data in this lab.
Add a compatible column before enforcing a new rule
-- SQLite; apply once after the preceding schemaALTER TABLE books ADD COLUMN shelf TEXT;UPDATE books SET shelf = 'general' WHERE shelf IS NULL;SELECT count(*) AS missing FROM books WHERE shelf IS NULL;Expected behavior: The query should return zero after backfill in the disposable fixture. Adding a final NOT NULL constraint requires a version-appropriate migration strategy; this example does not pretend to finish it.
Common mistake
Do not assume a TypeScript type enforces a database constraint. Do not add a mandatory column to a populated live table without planning compatibility and backfill. Do not assume a destructive migration can be reversed.
Practice and verification
Use the provided schema, attempt a valid insert and a rejected owner or quantity. Explain which rule is enforced by SQLite and which still belongs in an application authorization check. Draft an expand/backfill/contract plan.
Scope: App Router, proposed Next.js 16.x, React 19 family, Cache Components disabled. Framework snippets are static or explicitly partial; actual browser, auth and generated-route checks are pending. SQLite exercises run separately in the authoring environment, not the ByteMind evaluator.
Further reading: Sqlite Fk · Pg Constraints