When using PlanetScale with Prisma, the responsibility of applying the changes is on the PlanetScale side. This is different from the typical Prisma workflow which uses prisma migrate in order to generate SQL migrations for you based on changes in your Prisma schema. ![]() PlanetScale provides Online Schema Changes that are deployed automatically when you merge a deploy request and prevents blocking schema changes that can lead to downtime. We recommend prisma db push over prisma migrate dev for the following reasons: When prisma db push is run, it will ensure the schema in the PlanetScale branch you are currently connected to matches your current Prisma schema. Quick introduction to Prisma's db pushįrom a high level, Prisma's db push introspects your PlanetScale database to infer and execute the changes required to make your database schema reflect the state of your Prisma schema. In this tutorial, we're going to learn how to do Prisma migrations in PlanetScale as part of your deployment process using prisma db push. Also, you previously needed to turn on the ability to automatically copy the Prisma migration metadata. We cover Prisma + Nuxt 3 in more detail in the Mastering Nuxt 3 course if you want to learn more.This document has been updated to include the recommended Prisma and PlanetScale workflow, specifically the recommendation to use prisma db push instead of prisma migrate dev with shadow branches. We write one file that generates our database schema and all of the types for our app, making it so much easier to manage.Īnd Prisma comes with a ton of really great features - I highly recommend you check out the docs to see all of the different things you can do with it. The Prisma schema is the heart of what makes Prisma so great. UpdatedAt DateTime updatedAt Course Course relation (fields :, references : ) If we follow a similar process to link our Course and Chapter models, we end up with this schema: The other two fields are there to help Prisma hook everything up correctly, and are available in the client we’ll use in our TypeScript code. It’s important to note that we added three fields in order to set up these relationships, but only the chapterId field will actually be created in our database. We’re saying that the chapterId field on the Lesson model is linked to the id field on the Chapter model. chapter - This line tells Prisma exactly how this relationship operates. ![]() chapterId - Not only does the Chapter keep track of which Lesson objects it has, but each Lesson also needs to keep track of which Chapter it belongs to.These two fields we added do the following: Here, we add a relation between the Chapter and Lesson models:Ĭhapter Chapter relation (fields :, references : ) We need to represent this in our schema in order for Prisma (and Supabase/Postgres) to keep things consistent and ensure our data always makes sense. Our models are not completely independent though, and all the relationships between models is really the whole point of a relational database.Įach course contains multiple chapters, and each chapter multiple lessons. - automatically keeps track of the last time a record was updated.- specifies the default value of the field.- this is the unique id column for this table.Here are the ones used for the Course model: You can also specify native database types directly if you need to. Prisma has special types that map to Postgres, MySQL, and other database platforms. This is one of the locations that Prisma will look in by default.Ĭreate a schema.prisma file and add the following to it:įield Type Attributes. To keep things organized, we’ll create a /prisma folder where all things Prisma will live. Might as well get on with it, then! Creating our schema file If you’re unfamiliar with database schemas - and even if you are - getting used to Prisma schemas can take a bit of time.īut I promise that as we start to actually use the Prisma client, you’ll start to understand how this schema works even more clearly. Getting Data from our Database with Prisma.Creating the Prisma Schema □ we’re here.This is the second article in a series dedicated to showing you how to use Prisma in your Nuxt 3 app: It handles all of the boilerplate and coordination, so you just write one single schema that’s used in your database and in your TypeScript app. ![]() Trying to manage database schemas alongside your Nuxt app types can be a challenge.īut with Prisma, most of these problems go away.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |