Database migrations help you change your database structure safely over time. They keep your data organized as your app grows.
Database migrations in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
npx prisma migrate dev --name descriptive_migration_name
npx prisma migrate dev --name add_user_table
npx prisma migrate dev --name update_post_schema
npx prisma migrate deploy
This example shows how to add a new 'User' model to your Prisma schema and create a migration to update the database.
/*
1. Define your database schema in prisma/schema.prisma:
model User {
id Int @id @default(autoincrement())
name String
email String @unique
}
2. Run migration command in terminal:
npx prisma migrate dev --name add_user_model
3. This creates migration files and updates your database.
*/Always give your migration a clear, simple name describing the change.
Run migrations in development before deploying to production.
Use version control to keep track of migration files with your code.
Database migrations safely update your database structure as your app changes.
Use migration commands like npx prisma migrate dev in Next.js projects.
Keep migration files organized and named clearly for easy teamwork and deployment.
Practice
What is the main purpose of database migrations in a Next.js project?
Solution
Step 1: Understand database migrations
Database migrations are used to change the database schema safely without losing data.Step 2: Relate to Next.js usage
In Next.js, migrations help keep the database structure in sync with app changes.Final Answer:
To safely update the database structure as the app evolves -> Option BQuick Check:
Database migrations = safe schema updates [OK]
- Confusing migrations with UI or styling tasks
- Thinking migrations deploy the app
- Assuming migrations create app features
Which command is used to create and apply a new migration in a Next.js project using Prisma?
?
Solution
Step 1: Identify Prisma migration command
The commandnpx prisma migrate devcreates and applies migrations during development.Step 2: Differentiate from other commands
npm run buildbuilds the app,next devruns dev server, andnpx prisma generateregenerates Prisma client but does not migrate.Final Answer:
npx prisma migrate dev -> Option CQuick Check:
Migration command = npx prisma migrate dev [OK]
- Using 'npm run build' to migrate database
- Confusing 'npx prisma generate' with migration
- Running 'next dev' expecting migration
Given this Prisma schema change and migration command, what will happen?
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
// After adding a new field:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
isActive Boolean @default(true)
}
Command: npx prisma migrate dev --name add-isActiveSolution
Step 1: Understand schema change
A new Boolean fieldisActivewith defaulttrueis added to the User model.Step 2: Effect of migration command
The command creates a migration file that adds the new column with default value, so existing rows gettrueautomatically.Final Answer:
A new migration file is created and applied adding the isActive column with default true -> Option AQuick Check:
Adding field with default = migration applies safely [OK]
- Thinking migration fails due to missing data for new field
- Assuming schema does not change without manual SQL
- Believing app crashes immediately after migration
What is wrong with this migration command usage in a Next.js project?
npx prisma migrate dev --name
Solution
Step 1: Check command syntax
The--nameflag requires a migration name string after it.Step 2: Identify missing argument
Here, no name is provided, so the command will error out.Final Answer:
The migration name is missing after --name flag -> Option DQuick Check:
Migration name required after --name [OK]
- Omitting migration name after --name
- Confusing migrate dev with migrate deploy
- Using npm instead of npx unnecessarily
You want to add a new required field age Int to your User model in Prisma, but your database already has users without this field. What is the best way to handle this migration?
Solution
Step 1: Understand required field constraints
Adding a required fieldage Intdirectly will fail because existing rows lack this data.Step 2: Use a safe migration approach
First add the field as optional (age Int?), migrate, then update existing data, and finally change it to required.Final Answer:
Addage Int?as optional first, migrate, then backfill data, then make it required -> Option AQuick Check:
Make new required fields optional first [OK]
- Adding required field directly causing migration failure
- Deleting data unnecessarily
- Skipping migration and causing runtime errors
