{"id":2370,"date":"2026-07-24T00:38:28","date_gmt":"2026-07-23T17:38:28","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/24\/how-to-create-a-data-model-with-prisma-a-step-by-step-guide\/"},"modified":"2026-07-24T00:38:28","modified_gmt":"2026-07-23T17:38:28","slug":"how-to-create-a-data-model-with-prisma-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/24\/how-to-create-a-data-model-with-prisma-a-step-by-step-guide\/","title":{"rendered":"How to Create a Data Model with Prisma: A Step-by-Step Guide"},"content":{"rendered":"<h1>How to Create a Data Model with Prisma: A Step-by-Step Guide<\/h1>\n<p>Prisma is a modern ORM for Node.js and TypeScript that simplifies database interactions. Its core is the <code>schema.prisma<\/code> file where you define your data models. This tutorial walks you through creating a data model from scratch, covering key concepts like fields, relations, and migrations.<\/p>\n<p>First, initialize Prisma in your project using <code>npx prisma init<\/code>. This generates the schema file and a <code>.env<\/code> for your database connection string (e.g., PostgreSQL, SQLite). Open <code>prisma\/schema.prisma<\/code> to start modeling.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20create%20a%20data%20model%20with%20Prisma\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>1. Defining Your First Model<\/h2>\n<p>Add a <code>model<\/code> block with a PascalCase name (e.g., <code>User<\/code>). Inside, declare fields with name, type, and optional attributes.<\/p>\n<ul>\n<li><strong>Fields:<\/strong> <code>id Int @id @default(autoincrement())<\/code> \u2013 creates an auto-incrementing primary key.<\/li>\n<li><strong>Other types:<\/strong> <code>String<\/code>, <code>Boolean<\/code>, <code>DateTime<\/code>, <code>Float<\/code>.<\/li>\n<li><strong>Modifiers:<\/strong> <code>@unique<\/code> for unique constraints; <code>@default(...)<\/code> for default values.<\/li>\n<\/ul>\n<h2>2. Adding Relations Between Models<\/h2>\n<p>Prisma uses relational fields to connect models. For example, a <code>User<\/code> can have many <code>Post<\/code> records.<\/p>\n<ul>\n<li><strong>One-to-many:<\/strong> In <code>User<\/code>, add <code>posts Post[]<\/code> (list). In <code>Post<\/code>, add <code>author User @relation(fields: [authorId], references: [id])<\/code> and <code>authorId Int<\/code>.<\/li>\n<li><strong>Many-to-many:<\/strong> Use implicit relations by declaring a list on both sides (e.g., <code>tags Tag[]<\/code> and <code>posts Post[]<\/code>).<\/li>\n<\/ul>\n<h2>3. Running Migrations<\/h2>\n<p>Once your models are complete, sync them to your database:<\/p>\n<ul>\n<li>Run <code>npx prisma migrate dev --name init<\/code>. This generates SQL files and applies them.<\/li>\n<li>Check your database (via a GUI or CLI) \u2013 the tables should match your schema.<\/li>\n<li>Use <code>npx prisma generate<\/code> to create the Prisma Client, which provides type-safe queries.<\/li>\n<\/ul>\n<p><strong>Conclusion:<\/strong> You\u2019ve now created a data model with Prisma. The schema file is your single source of truth \u2013 update it and run migrations to evolve your database. Start querying with <code>prisma.user.findMany()<\/code> and build your application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Create a Data Model with Prisma: A Step-by-Step Guide Prisma is a modern ORM for Node.js and TypeScript that simplifies database interactions. Its core is the schema.prisma file where you define your data models. This tutorial walks you through creating a data model from scratch, covering key concepts like fields, relations, and migrations. &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-2370","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=2370"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2370\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=2370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=2370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=2370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}