Bird
Raised Fist0
NextJSframework~20 mins

Database migrations in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Database Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding migration effect on Next.js app

You have a Next.js app using Prisma for database migrations. After running prisma migrate dev, what happens to your database schema?

AThe Prisma schema file is overwritten with the current database schema.
BThe database schema resets to an empty state, deleting all data.
CNothing changes until you restart the Next.js server.
DThe database schema updates to match the Prisma schema, and a new migration file is created.
Attempts:
2 left
💡 Hint

Think about what prisma migrate dev does to keep your database and Prisma schema in sync.

📝 Syntax
intermediate
2:00remaining
Identify the correct migration script syntax

Which of the following Prisma migration schema snippets correctly adds a new email field to a User model?

Amodel User { id Int @id @default(autoincrement()) email String? }
Bmodel User { id Int @id @default(autoincrement()) email Boolean }
Cmodel User { id Int @id @default(autoincrement()) email String }
Dmodel User { id Int @id @default(autoincrement()) email Int }
Attempts:
2 left
💡 Hint

Consider the data type for an email address and whether it should be optional.

🔧 Debug
advanced
2:00remaining
Fix migration error due to conflicting schema changes

You run prisma migrate dev but get an error about conflicting migrations. What is the likely cause?

AYou have two migration files that change the same table in incompatible ways.
BYour Prisma client is outdated and needs to be reinstalled.
CYou forgot to restart the Next.js server after schema changes.
DYour database connection string is missing in the .env file.
Attempts:
2 left
💡 Hint

Think about what happens when multiple migrations try to change the same part of the database.

state_output
advanced
2:00remaining
Result of migration rollback in Next.js Prisma setup

After applying several migrations, you run prisma migrate reset. What is the state of your database?

AOnly the last migration is undone, keeping previous migrations intact.
BThe database is dropped, recreated, and all migrations are reapplied from scratch.
CThe database schema stays the same but all data is deleted.
DNothing happens unless you manually delete migration files.
Attempts:
2 left
💡 Hint

Consider what prisma migrate reset does to the database and migrations.

🧠 Conceptual
expert
2:00remaining
Choosing migration strategy for production Next.js app

You want to deploy a Next.js app with Prisma to production. Which migration strategy ensures safe schema updates without data loss?

AUse <code>prisma migrate deploy</code> to apply tested migration files without resetting the database.
BRun <code>prisma migrate reset</code> on production to ensure a clean schema.
CManually edit the database schema and skip Prisma migrations.
DDelete all migration files and generate new ones on production.
Attempts:
2 left
💡 Hint

Think about how to safely update a live database without losing data.

Practice

(1/5)
1.

What is the main purpose of database migrations in a Next.js project?

easy
A. To deploy the Next.js app to the server
B. To safely update the database structure as the app evolves
C. To create user interfaces automatically
D. To write CSS styles for the app

Solution

  1. Step 1: Understand database migrations

    Database migrations are used to change the database schema safely without losing data.
  2. Step 2: Relate to Next.js usage

    In Next.js, migrations help keep the database structure in sync with app changes.
  3. Final Answer:

    To safely update the database structure as the app evolves -> Option B
  4. Quick Check:

    Database migrations = safe schema updates [OK]
Hint: Migrations change database structure safely [OK]
Common Mistakes:
  • Confusing migrations with UI or styling tasks
  • Thinking migrations deploy the app
  • Assuming migrations create app features
2.

Which command is used to create and apply a new migration in a Next.js project using Prisma?

?
easy
A. next dev
B. npm run build
C. npx prisma migrate dev
D. npx prisma generate

Solution

  1. Step 1: Identify Prisma migration command

    The command npx prisma migrate dev creates and applies migrations during development.
  2. Step 2: Differentiate from other commands

    npm run build builds the app, next dev runs dev server, and npx prisma generate regenerates Prisma client but does not migrate.
  3. Final Answer:

    npx prisma migrate dev -> Option C
  4. Quick Check:

    Migration command = npx prisma migrate dev [OK]
Hint: Use 'npx prisma migrate dev' to create and apply migrations [OK]
Common Mistakes:
  • Using 'npm run build' to migrate database
  • Confusing 'npx prisma generate' with migration
  • Running 'next dev' expecting migration
3.

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-isActive
medium
A. A new migration file is created and applied adding the isActive column with default true
B. The migration fails because isActive is missing in existing rows
C. The database schema remains unchanged
D. The app crashes due to schema mismatch

Solution

  1. Step 1: Understand schema change

    A new Boolean field isActive with default true is added to the User model.
  2. Step 2: Effect of migration command

    The command creates a migration file that adds the new column with default value, so existing rows get true automatically.
  3. Final Answer:

    A new migration file is created and applied adding the isActive column with default true -> Option A
  4. Quick Check:

    Adding field with default = migration applies safely [OK]
Hint: Adding field with default creates migration safely [OK]
Common Mistakes:
  • Thinking migration fails due to missing data for new field
  • Assuming schema does not change without manual SQL
  • Believing app crashes immediately after migration
4.

What is wrong with this migration command usage in a Next.js project?

npx prisma migrate dev --name
medium
A. The command is correct and will run successfully
B. The command should be run with npm, not npx
C. The command should be prisma migrate deploy instead
D. The migration name is missing after --name flag

Solution

  1. Step 1: Check command syntax

    The --name flag requires a migration name string after it.
  2. Step 2: Identify missing argument

    Here, no name is provided, so the command will error out.
  3. Final Answer:

    The migration name is missing after --name flag -> Option D
  4. Quick Check:

    Migration name required after --name [OK]
Hint: Always provide a name after --name flag [OK]
Common Mistakes:
  • Omitting migration name after --name
  • Confusing migrate dev with migrate deploy
  • Using npm instead of npx unnecessarily
5.

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?

hard
A. Add age Int? as optional first, migrate, then backfill data, then make it required
B. Add age Int directly and run migration; it will succeed automatically
C. Delete all existing users before migration to avoid errors
D. Skip migration and add the field only in the app code

Solution

  1. Step 1: Understand required field constraints

    Adding a required field age Int directly will fail because existing rows lack this data.
  2. 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.
  3. Final Answer:

    Add age Int? as optional first, migrate, then backfill data, then make it required -> Option A
  4. Quick Check:

    Make new required fields optional first [OK]
Hint: Add new required fields as optional first [OK]
Common Mistakes:
  • Adding required field directly causing migration failure
  • Deleting data unnecessarily
  • Skipping migration and causing runtime errors