Prisma @map and @@map to SQL Table and Column Names

Learn how Prisma @map and @@map annotations control the generated SQL table and column names. Understand the naming convention differences between Prisma and SQL.

Advanced

Detailed Explanation

Name Mapping with @map and @@map

Prisma uses PascalCase for model names and camelCase for field names, while SQL conventionally uses snake_case. The @map and @@map annotations bridge this gap by specifying the exact database name.

Example Prisma Schema

model UserProfile {
  id          Int      @id @default(autoincrement())
  firstName   String   @map("first_name")
  lastName    String   @map("last_name")
  dateOfBirth DateTime @map("date_of_birth")
  avatarUrl   String?  @map("avatar_url")
  isVerified  Boolean  @default(false) @map("is_verified")

  @@map("user_profiles")
}

Generated SQL

CREATE TABLE user_profiles (
  id SERIAL PRIMARY KEY,
  first_name VARCHAR(255) NOT NULL,
  last_name VARCHAR(255) NOT NULL,
  date_of_birth TIMESTAMP NOT NULL,
  avatar_url VARCHAR(255),
  is_verified BOOLEAN NOT NULL DEFAULT FALSE
);

Automatic vs Explicit Naming

The converter handles naming in two ways:

  1. With @map/@@map: Uses the specified name exactly as-is.
  2. Without mapping: Automatically converts PascalCase/camelCase to snake_case.
Prisma name Without @map With @map("custom_name")
UserProfile (model) user_profile Whatever @@map specifies
firstName (field) first_name Whatever @map specifies
dateOfBirth (field) date_of_birth Whatever @map specifies

When to Use @map

  • Legacy databases: When connecting Prisma to an existing database with non-standard naming.
  • Conventions: When your SQL naming convention differs from automatic snake_case conversion.
  • Reserved words: When a natural field name conflicts with a SQL reserved word, @map lets you use a different database name.

Combined Example

model OrderItem {
  id        Int @id @default(autoincrement())
  orderId   Int @map("order_id")
  productId Int @map("product_id")
  qty       Int @map("quantity")
  unitPrice Decimal @map("unit_price")

  @@map("order_items")
}

Here, qty maps to quantity in the database, demonstrating that @map can remap to any name, not just the snake_case equivalent.

Use Case

You are connecting Prisma to a legacy database with established naming conventions that don't follow standard snake_case, and you need to verify that the @map annotations correctly produce the expected SQL column and table names.

Try It — Prisma to SQL Schema

Open full tool