AutoMigrate
GORM-style automatic schema migration. Creates tables, adds missing columns and indexes. Never deletes or changes existing data.
How It Works
AutoMigrate inspects your model definitions and the database schema, then:
- Creates table if it doesn't exist
- Adds missing columns (never deletes or changes types)
- Creates missing indexes for
uniqueandindexcolumns - Creates history table if model has
versioned: true
This is safe for development and production. You can also use migrate:auto as a CLI command.
Primary Keys
The column you declare as id becomes the table's primary key, and its declared type is honoured:
INTEGERorBIGINTgets the database's auto-increment key (AUTOINCREMENT,AUTO_INCREMENTorSERIAL), and the database assigns it.- Anything else —
STRING,UUID— is created as aNOT NULL PRIMARY KEYof that type, for you to supply. This is the patterngenerate:modelscaffolds.
A string id must stay a string
A UUID column quietly created as an integer would reject every create({ id: generateUUID() }) with datatype mismatch, so the declared type wins. Since AutoMigrate only ever adds, a table created before this rule existed keeps its old key — drop it (or write a migration) to pick up the new definition.
Usage
CLI Command
Scans models/ directory and auto-migrates all found models.
AutoMigrate vs Migrations
You don't need to disable migrations.
AutoMigrate and manual migrations can coexist. They serve different purposes:
| Feature | AutoMigrate | Migrations |
|---|---|---|
| Use case | Development, prototyping | Production, version control |
| Direction | Forward only (add columns) | Up and down (rollback) |
| Tracked | No tracking table | stabilize_migrations table |
| Rollback | Not supported | Full rollback support |
| Safe for prod | Yes (never deletes) | Yes (reviewed SQL) |
Recommended approach:
- Use
autoMigrateduring development for rapid iteration - Use
generate:migration+migratefor production deployments - Both work together — AutoMigrate adds columns, migrations track history
- AutoMigrate is safe — it never deletes data or drops columns
What AutoMigrate Does NOT Do
- Does NOT delete columns
- Does NOT change column types
- Does NOT rename columns
- Does NOT drop tables
- Does NOT create a rollback mechanism
For destructive changes, use manual migrations with explicit up and down SQL.