Course

Updating Our Schema

When you're building an application, your database is like a blueprint that defines the structure of your database. It specifies the tables you have, the columns each table contains, and the relationships between those tables.

However, as your application evolves, you might need to make changes to this schema. Maybe you realize you need an additional column in a table, or perhaps you want to create a new table altogether.

With , there are two different commands used to make changes to your .

  • db push
  • migrate

db push

npx prisma db push

The db push command is a quick and easy way to update your database schema directly from your Prisma schema file. When you run npx prisma db push, Prisma takes a look at your schema file and compares it to your actual database. If there are any differences, it automatically updates your database to match the schema.

Think of it like this: imagine you're an artist working on a painting. As you're painting, you realize you want to add a new element to the picture. With db push, you simply make the change to your sketch (the Prisma schema), and then Prisma magically updates the actual painting (your database) to include that new element. It's a straightforward way to keep your database in sync with your schema.

However, there's a catch with db push. It's intended for development purposes only. It's great for quickly iterating on your schema during development, but it's not recommended for production use. Why? Because db push doesn't create any migration files, which are essential for tracking and managing schema changes over time.

migrate

npx prisma migrate dev

That's where the migrate command comes in. When you're ready to make schema changes in a more structured and production-friendly way, you'll want to use npx prisma migrate dev. This command is like a more powerful version of db push.

When you run npx prisma migrate dev, Prisma once again compares your schema file to your database. But instead of directly updating the database, it generates a migration file. This migration file is like a set of instructions that describe the specific changes needed to update your database schema.

Think of it like writing down the steps to update your painting. Instead of directly making changes to the painting, you create a separate list of instructions that detail the changes you want to make. This way, you have a record of what changes were made and when.

So, to recap:

  • Use db push for quick schema iterations during development.
  • Use migrate for structured and production-ready schema changes.

In the following lessons and modules, we will be actively developing the application and may need to make many changes to our , so we'll be primarily using the command to keep our database in sync with our schema.

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Introduction To The Project