Evolving prisma schema

Share this video with your friends

Send Tweet
Published 2 years ago
Updated a year ago

In most of cases your database schema won't stay the same for too long - let's learn how to update the prisma client so it has access to all of the additions made to the postgress

Dimitri Ivashchuk: [0:01] Let's see how we can evolve Prisma schema. We are in the docker-compose file where we are setting up our database. The database is running, so we need to first stop it with docker-compose down.

[0:16] When the Postgres database is downed, we need to clean the database from volumes, so we need to run the ./clean.sh script. The database has been cleaned. We just add some stuff to our .sql.

[0:35] Just to be clear, we create a couple of tables with a couple of relations. In this case, we add the Posts and Profiles as relations to User table. Let's save this and run docker-compose up. Our Postgres database should be running again. Let's see with psql prisma postgres.

[1:05] Now, we want to list all the tables. We see that indeed we have the Post, Profile, and User tables inside. Now we can work with our Prisma file.

[1:17] We are in schema.prisma file from our previous work and now we want to update it according to our database. We need to run npx prisma introspect.

[1:32] You see that our schema.prisma file has changed. We now have the Profile and Post in addition to the User. We have all the table fields which we have specified in our .sql file when booting up the database.

[1:51] The only thing which is not in place now is the correct name and convention. We can luckily fix that. First of all, we want to rename the User relation to author to better deliver the semantics. We also want to lowercase this User because again, it's relation. Then post would become posts because it conveys the meaning that it's a list of posts. Profile becomes profile with the lowercase.

[2:27] Our Prisma model in place, we can now save the file, open terminal, clear it, and run npx prisma generate, which will generate a new Prisma client for us. That's it for evolving Prisma schema.