0
0
NestJSframework~10 mins

Prisma migrations in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Prisma migrations
Write schema.prisma
Run prisma migrate dev
Migration files created
Database schema updated
Application uses updated schema
Repeat for new changes
This flow shows how you write your Prisma schema, run migrations to update the database, and then use the updated schema in your NestJS app.
Execution Sample
NestJS
model User {
  id    Int    @id @default(autoincrement())
  email String @unique
  name  String?
}

// Run: npx prisma migrate dev --name init
Defines a User model and runs a migration to create the corresponding database table.
Execution Table
StepActionInput/CommandEffectOutput/Result
1Write schemaAdd User model to schema.prismaSchema file updatedUser model defined
2Run migrationnpx prisma migrate dev --name initMigration process startsMigration files created
3Apply migrationDatabase updatedDatabase schema matches User modelUser table created
4Generate clientPrisma client updatedClient reflects new schemaClient ready to use
5Use in appNestJS uses Prisma clientApp can query User tableData operations succeed
6ExitNo more migrationsProcess endsDatabase and app in sync
💡 No more schema changes, migration process complete
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
schema.prismaempty or old schemaUser model addedUser model unchangedUser model unchangedUser model unchangedUser model defined
migration filesnonenoneinit migration createdinit migration appliedinit migration appliedinit migration applied
database schemaold or emptyold or emptyold or emptyUser table createdUser table createdUser table created
Prisma clientold versionold versionold versionupdated with User modelupdated with User modelupdated with User model
Key Moments - 2 Insights
Why do we need to run 'prisma migrate dev' after changing schema.prisma?
Because the migration command creates files and updates the database schema to match your Prisma schema. Without it, the database won't reflect your changes (see execution_table step 2 and 3).
What happens if you change schema.prisma but don't generate the Prisma client?
Your app will use an outdated client that doesn't know about new models or fields. Step 4 in the execution_table shows client generation is needed to keep the client in sync.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is created at step 2?
ADatabase tables
BMigration files
CPrisma client
DNestJS modules
💡 Hint
Check the 'Effect' and 'Output/Result' columns in step 2 of the execution_table.
At which step does the database schema get updated?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look for 'Database schema matches User model' in the 'Effect' column.
If you skip step 4, what problem will you face?
AMigration files won't be created
BDatabase schema won't update
CPrisma client won't reflect schema changes
DNestJS app won't start
💡 Hint
Refer to the 'Prisma client' variable in variable_tracker after step 4.
Concept Snapshot
Prisma migrations let you update your database schema safely.
1. Edit schema.prisma to define models.
2. Run 'npx prisma migrate dev --name <desc>' to create migration files and update DB.
3. Prisma client regenerates to match schema.
4. Use updated client in NestJS app.
Repeat for every schema change.
Full Transcript
Prisma migrations in NestJS start by writing or updating the schema.prisma file to define your data models. Then, you run the command 'npx prisma migrate dev --name <migration_name>' which creates migration files and applies changes to your database schema. After the database updates, Prisma regenerates its client to reflect the new schema. Finally, your NestJS application uses this updated client to interact with the database. This process repeats whenever you change your data models, ensuring your database and app stay in sync.