Schema
Learn how to update your database schema and migrate changes with Prisma in Achromatic.
The database schema is defined in packages/database/prisma/schema.prisma
. This schema file uses Prisma’s schema definition language to describe your database tables, relationships, and types.
Update
To update your database schema, you can edit the schema.prisma
file located in the /packages/database
package. More information about the Prisma schema file can be found here.
For example we can add a new field businessPhone
to the Contact
model.
model Contact {
...
businessPhone String? @db.VarChar(32)
...
}
That's it, we have defined a new field. The field is an optional string with a max length of 32 characters. Now let's tell the database about it using a migration
.
Migration
Create a new migration by running the following command:
pnpm --filter database migrate
This will keep a migration history of your database changes. Prisma creates a new migrations under packages/database/migrations
.
Push changes to the database
To apply your schema changes to the database, run:
pnpm --filter database push
After the migration is applied, Prisma creates a new migration history record in the database.
Generate prisma client
After pushing your schema changes, re-generate the Prisma client by running:
pnpm --filter database generate
This command will automatically be executed when you start the development server or build the project.
The TypeScript server has sometimes a problem with re-discovering the newly generated type definitions. Restarting VS Code usually does the trick, but you can also restart the TypeScript server.