database-model

xmark168's avatarfrom xmark168

Create Prisma schema models with relations and indexes. Use when designing database schemas, adding models, or defining entity relationships.

⭐3stars🔀0forks📁View on GitHub🕐Updated Dec 30, 2025

When & Why to Use This Skill

This Claude skill automates the generation of Prisma schema models, providing a structured approach to database design. It ensures that entity relationships, indexing strategies, and naming conventions follow industry best practices, significantly reducing manual coding errors and accelerating the setup of robust data layers for modern web applications.

Use Cases

  • Initial Schema Design: Quickly architecting a new database by defining core models, field types, and primary keys using standardized UUID defaults.
  • Relationship Management: Automatically configuring complex database relations, including one-to-one, one-to-many, and many-to-many associations with proper foreign key constraints and cascading deletes.
  • Performance Optimization: Enforcing the use of indexes on foreign keys and selecting optimal numeric types (Int, Float, or Decimal) to ensure high-performance queries and data integrity.
  • Standardizing Development: Maintaining project consistency by enforcing PascalCase for models, camelCase for fields, and mandatory timestamp tracking for all data entities.
namedatabase-model
descriptionCreate Prisma schema models with relations and indexes. Use when designing database schemas, adding models, or defining entity relationships.

⚠️ CRITICAL - DO NOT MODIFY

// prisma/schema.prisma - NEVER change these blocks
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Import: import { prisma } from '@/lib/prisma' (already exists)

Model Conventions

model Product {
  id        String   @id @default(uuid())
  name      String
  price     Float    // Use Float for UI (returns number directly)
  isActive  Boolean  @default(true)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  category   Category @relation(fields: [categoryId], references: [id], onDelete: Cascade)
  categoryId String

  @@index([categoryId])
  @@index([name])
}

Rules:

  • Model names: PascalCase
  • Field names: camelCase
  • IDs: @id @default(uuid())
  • Always: createdAt, updatedAt
  • Always: @@index on foreign keys

Number Types

Type Returns Use Case
Int number Counts, whole numbers
Float number Prices (recommended)
Decimal Prisma.Decimal Financial (needs conversion)

Relations

One-to-Many

model Category {
  id       String    @id @default(uuid())
  name     String    @unique
  products Product[]
}

model Product {
  id         String   @id @default(uuid())
  category   Category @relation(fields: [categoryId], references: [id], onDelete: Cascade)
  categoryId String
  @@index([categoryId])
}

One-to-One

model User {
  id      String   @id @default(uuid())
  profile Profile?
}

model Profile {
  id     String @id @default(uuid())
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId String @unique  // @unique = one-to-one
}

Many-to-Many

model Post {
  id   String @id @default(uuid())
  tags Tag[]  // Prisma handles junction table
}

model Tag {
  id    String @id @default(uuid())
  name  String @unique
  posts Post[]
}

NextAuth Models

User, Account, Session already exist in boilerplate. Just add relations:

model User {
  // ... existing fields
  posts Post[]  // Add your relations
}

After Schema Changes

DO NOT run db push manually. Runs automatically in validation phase.

NEVER

  • Modify datasource/generator blocks
  • Recreate lib/prisma.ts
  • Recreate User/Account/Session
  • Forget @@index on foreign keys
  • Skip onDelete: Cascade on children
  • Run db push during implementation