# Changing the content model on a database that has data

URL: https://kywi-docs-production.up.railway.app/developing/changing-the-content-model-on-a-database-that-has-data
Type: docPage

Schema generation reads config types and admin-created types together, so running it against a half-readable database is what offers to drop live tables.

`kywi migrate` is not one command. It spawns `kywi migrate:generate` and aborts if that fails, pre-flights the database, refuses to continue if the generated schema does not cover what is live, runs `drizzle-kit generate` to capture the diff as a migration file, then applies it. Two of those steps stop you on purpose; those are the ones worth knowing.

## Generation reads the database, not just the config

Types created in the Type Designer live as rows but own real `kywi_ct_*` tables, so a schema derived from `kywi.config.ts` alone describes only part of the content model. Generation therefore connects, lists what is actually there, and writes those runtime-managed tables into `drizzle.config.ts` as exclusions, which keeps drizzle-kit from proposing to delete them.

Reachability is probed on its own, before anything is read, and that distinction is the safety property: an **unreachable** database means an offline build, so generation falls back to a config-only schema with a warning. A database that answers `select 1` and then fails the read is a different situation, and the command aborts rather than emit a schema that omits — and so un-protects — those tables.

## This is the path that offers to drop live tables

An unprotected schema hands drizzle-kit a content model missing every admin-created table, and drizzle-kit's response to a table it cannot see in the schema is to offer to `DROP` it — with its rows. The same hazard applies inside a runtime-managed table: deleting a field there never drops its column, so the leftovers look like deletions to a diff. Answering "yes" to a drizzle-kit prompt you did not read is how a content type's data disappears.

## The guard that refuses to run drizzle-kit

Before spawning drizzle-kit at all, `kywi migrate` checks every live `kywi_ct_*` table against the generated output: each one must either have its own schema file or be negated in `drizzle.config.ts`. If any is neither, it stops:

✖ Refusing to run drizzle-kit: 1 admin-created content-type table(s) exist in the database but are missing from the generated schema: kywi_ct_property Running the migration now would offer to DROP these tables and their rows (#38). Re-run 'kywi migrate' with a DATABASE_URL that can read kywi_content_types, or report this as a bug if the problem persists.

This message can only be reached after step 1 (`migrate:generate`) has already exited 0: a database that is reachable but fails the admin-type read aborts inside `migrate:generate` itself, and `kywi migrate` exits on that non-zero status without ever reaching this guard. What actually reaches this guard is the config-only fallback itself, the one cause the guard's own comment names: the database was unreachable when `migrate:generate` ran but answers this guard's own re-probe moments later inside `kywi migrate` — or `migrate:generate` hit a bug that made it fall back to a config-only schema despite the database being reachable. Either way, the fix is the same: re-run `kywi migrate` against a database connection that can actually read `kywi_content_types` at generation time.

## Renaming a field

Nothing carries a field's previous name — the column is derived from `field.name` alone. Rename a field and the generator sees one column gone and one new — the one diff drizzle-kit cannot resolve on its own. It asks whether the column was created or renamed from another, and with no terminal attached the prompt resolves nothing and drizzle-kit **exits successfully having written no migration at all**. `kywi migrate` reads the output rather than the exit code, recognises the prompt, and fails loudly.

So plan a rename as three deploys — add the new field, backfill it, drop the old one — or resolve the prompt yourself in an interactive terminal and commit the migration it writes. [When migrate can't resolve itself: ambiguous renames](/operating/when-migrate-cant-resolve-itself-ambiguous-renames/) has the operator's version.

## Read the migration before it is applied anywhere

The generated SQL is a diff against drizzle's own snapshot, not against your database, and the two can disagree — a generated migration has arrived carrying an `ADD COLUMN` for a column the database already had, which fails on contact. Editing a not-yet-applied file is safe, because it is drizzle-orm's own migrator that hashes each `.sql` file at apply time (`readMigrationFiles`, sha256 over the file content) rather than trusting anything recorded when it was generated — so what you edit today is exactly what gets applied and recorded. Editing an applied one is not. Commit the migration and the snapshot together, or the next generate diffs against a snapshot nobody else has.

## Why --push is awkward on purpose

`--push` writes straight to the database and never touches the journal, so on a project that already has one it demands `--force-push` alongside it — an explicit acknowledgement that later journal-based migrations may then fail or silently skip the change. On a project with no journal yet it is the intended prototyping path and needs no flag. Neither case belongs in a deploy — see [Why drizzle-kit push is banned before deploy](/operating/why-drizzle-kit-push-is-banned-before-deploy/), [Running migrations against a live database](/operating/running-migrations-against-a-live-database/), and [Post-migration data steps](/operating/post-migration-data-steps-and-why-they-can-silently-not-run/) for the ones a successful migrate runs afterwards.
