<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sagar Kumar</title>
    <description>The latest articles on DEV Community by Sagar Kumar (@sagar_dewani).</description>
    <link>https://dev.to/sagar_dewani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2901690%2F7734cf8d-d391-400e-8797-e36e9c8b7b42.jpg</url>
      <title>DEV Community: Sagar Kumar</title>
      <link>https://dev.to/sagar_dewani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagar_dewani"/>
    <language>en</language>
    <item>
      <title>Getting to Know Prisma: A Beginner’s Guide with Examples</title>
      <dc:creator>Sagar Kumar</dc:creator>
      <pubDate>Thu, 27 Feb 2025 18:26:50 +0000</pubDate>
      <link>https://dev.to/sagar_dewani/getting-to-know-prisma-a-beginners-guide-with-examples-48l5</link>
      <guid>https://dev.to/sagar_dewani/getting-to-know-prisma-a-beginners-guide-with-examples-48l5</guid>
      <description>&lt;p&gt;Prisma is an incredible tool for developers working with databases, offering a modern &lt;strong&gt;ORM&lt;/strong&gt; (Object-Relational Mapping) experience. Recently, I dove into Prisma with &lt;strong&gt;PostgreSQL&lt;/strong&gt; for a project and found myself exploring its features hands-on. One task stood out: adding a new model to my Prisma schema and syncing it with my PostgreSQL database. While Prisma’s documentation is thorough, it can feel overwhelming. So, I’m sharing my simplified explanation—along with examples—based on my journey.&lt;/p&gt;

&lt;p&gt;In this article, I'll cover how to add a new model, the difference between &lt;code&gt;prisma migrate dev&lt;/code&gt;and &lt;code&gt;prisma migrate deploy&lt;/code&gt;, and why choosing the right command matters depending on your environment.&lt;/p&gt;

&lt;h1&gt;
  
  
  Let's Add a New Model: The Starting Point
&lt;/h1&gt;

&lt;p&gt;Let’s say I’m building a blog app and already have a &lt;code&gt;User&lt;/code&gt; model in my &lt;code&gt;schema.prisma&lt;/code&gt; file. Now, I want to add a &lt;code&gt;Post&lt;/code&gt; model so users can create blog posts. Here’s what my initial &lt;code&gt;schema.prisma&lt;/code&gt; might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id    Int     @id @default(autoincrement())
  name  String
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To add the &lt;code&gt;Post&lt;/code&gt; model, I update the &lt;code&gt;schema.prisma&lt;/code&gt; file like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id    Int     @id @default(autoincrement())
  name  String
  posts Post[]  // Relation to Post model
}

model Post {
  id        Int     @id @default(autoincrement())
  title     String
  content   String
  authorId  Int
  author    User    @relation(fields: [authorId], references: [id])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding the &lt;code&gt;Post&lt;/code&gt; model, I ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx prisma generate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This updates the Prisma Client with the new model, but my PostgreSQL database doesn’t reflect this change yet. To sync the database, I need to use Prisma’s migration commands. &lt;br&gt;
That’s where &lt;code&gt;prisma migrate dev&lt;/code&gt; and &lt;code&gt;prisma migrate deploy&lt;/code&gt; come in—but &lt;strong&gt;which one should I use, and why&lt;/strong&gt;?&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding &lt;code&gt;prisma migrate dev&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Since I’m working locally, I opted for &lt;code&gt;prisma migrate dev&lt;/code&gt;. Here’s what it does, step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Detects Schema Drifts&lt;/strong&gt;: It compares my &lt;code&gt;schema.prisma&lt;/code&gt; file with the actual database structure. If there’s a mismatch (e.g., a missing table or column), it suggests changes. Be cautious here—applying these suggestions unwisely can lead to data loss. (I’ll cover how to handle this safely in a future article.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Generates a Migration File&lt;/strong&gt;: It creates a SQL migration file in the prisma/migrations folder based on the changes. For my &lt;code&gt;Post&lt;/code&gt; model, the file might look like this:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"Post"&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;"id"&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"title"&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"content"&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"authorId"&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;FOREIGN&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"authorId"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nv"&gt;"User"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Applies the Migration&lt;/strong&gt;: It runs the SQL against my local PostgreSQL database, creating the &lt;code&gt;Post&lt;/code&gt; table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Saves Migration History&lt;/strong&gt;: It stores a record of the migration (with a &lt;code&gt;SHA256&lt;/code&gt; hash) in a special &lt;code&gt;_prisma_migrations&lt;/code&gt; table. This tracks which migrations have been applied.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Updates the Prisma Client&lt;/strong&gt;: It regenerates the &lt;code&gt;Prisma Client&lt;/code&gt; to include the new &lt;code&gt;Post&lt;/code&gt; model, so I can query it in my code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To run this, I used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx prisma migrate dev &lt;span class="nt"&gt;--name&lt;/span&gt; add_post_model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;--name&lt;/code&gt; flag gives the migration a descriptive name (e.g., &lt;code&gt;add_post_model&lt;/code&gt;). After running it, I can now use the &lt;code&gt;Post&lt;/code&gt; model in my app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PrismaClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@prisma/client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrismaClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createPost&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;My First Post&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, Prisma!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;authorId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;createPost&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command is perfect for local development because it handles everything—drift detection, migration creation, and client updates—in one go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use &lt;code&gt;prisma migrate deploy&lt;/code&gt; Elsewhere?
&lt;/h2&gt;

&lt;p&gt;Now, imagine I’m ready to push my app to production or a testing environment. Here, &lt;code&gt;prisma migrate dev&lt;/code&gt; isn’t the right choice. Instead, I use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx prisma migrate deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s why: &lt;code&gt;prisma migrate deploy&lt;/code&gt; is more limited—and that’s intentional. It only does two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Applies Existing Migration Files&lt;/strong&gt;: It runs the SQL files in the &lt;code&gt;prisma/migrations&lt;/code&gt; folder against the database, in the order they were created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Updates the Migration History&lt;/strong&gt;: It ensures the &lt;code&gt;_prisma_migrations&lt;/code&gt; table reflects the applied migrations.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It skips drift detection, migration file generation, and Prisma Client regeneration (Steps 1, 2, 4, and 5 from migrate dev). This makes it safer and more predictable in production:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Drift Detection&lt;/strong&gt;: Production databases shouldn’t be auto-corrected based on schema drift. That could lead to unintended changes or data loss.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No New Migrations&lt;/strong&gt;: Migration files should be created and tested locally, then committed to version control—not generated on the fly in production.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Client Regeneration&lt;/strong&gt;: The Prisma Client is typically built during the app’s deployment process, not during migration.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, after running &lt;code&gt;prisma migrate dev&lt;/code&gt; locally and committing the migration files, I deploy my app to production. On the production server, I run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx prisma migrate deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This applies the &lt;code&gt;add_post_model&lt;/code&gt; migration to the production PostgreSQL database, ensuring it matches my local setup without extra steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;prisma migrate dev&lt;/code&gt; in development to create and apply migrations, detect drifts, and update the Prisma Client. It’s a full-featured tool for experimenting locally.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;prisma migrate deploy&lt;/code&gt; in production or testing to apply pre-existing migrations safely, without altering the database beyond what’s planned.&lt;br&gt;
Always test migrations locally first to avoid surprises in production.&lt;br&gt;
Prisma’s migration system is powerful, but the distinction between these commands confused me at first. The official docs are detailed, but I hope this simpler breakdown—complete with examples—helps you get started faster. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stay tuned for my next article on preventing data loss during migrations!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some Important Links:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.prisma.io/docs/orm/reference/prisma-cli-reference#migrate-dev" rel="noopener noreferrer"&gt;Prisma Migrate Dev&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.prisma.io/docs/orm/reference/prisma-cli-reference#migrate-deploy" rel="noopener noreferrer"&gt;Prisma Migrate Deploy&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>prisma</category>
      <category>webdev</category>
      <category>node</category>
      <category>migrate</category>
    </item>
  </channel>
</rss>
