skip to content

In Prisma 7, how do @map and @@map let a schema keep PascalCase models and camelCase fields over existing snake_case tables and columns?

level: middleimportance: should knowfreq 42%

answer

  1. two names for one thing
  2. field level versus block level
  3. client keeps the schema names
  4. default index names use table names

basics

~20 s

@map("created_at") binds a field or enum value to a differently named column or stored value; @@map("users") does the same for a model's table or an enum's type. Prisma Client keeps the schema names; only SQL uses the mapped ones.

solid answer

~50 s

Both attributes record the database's name next to the name you want in code; neither renames anything by itself. `@map` sits on a field (`createdAt DateTime @map("created_at")`) or an enum value, and `@@map` sits on a model (`@@map("users")`) or an enum block. Prisma Client uses only the schema names, so you write `prisma.user.create({ data: { createdAt } })` while the SQL targets `users.created_at`. For enums, `@map` changes the stored value, but the generated TypeScript constant keeps the schema name: `Role.AUTHOR` is `"AUTHOR"` even when the database stores `author`. Default constraint and index names are built from the **database** names, so a mapped `User` gets `users_pkey` and `users_email_key`. On a database that Prisma Migrate already manages, changing a mapped name is a schema change, and the generated SQL for a new `@@map` is a drop and re-create, not a rename.

code

sql · 10 lines
sql
-- DropTable
DROP TABLE "users";

-- CreateTable
CREATE TABLE "app_users" (
    "id" SERIAL NOT NULL,
    "given_name" TEXT NOT NULL,

    CONSTRAINT "app_users_pkey" PRIMARY KEY ("id")
);

go deeper

for a junior

Remember the split: @map is for fields and enum values, @@map is for models and enums, and the client always uses the schema names.

for a middle

Explain how introspection plus @map and @@map adopts a snake_case database, why enum constants keep schema names, and how default constraint names come from table names.

for a senior

Show you would catch the drop-and-create SQL that a changed @@map generates, and that you check raw queries and constraint names against the database names, not the schema names.

for a principal

Weigh keeping legacy database names behind mappings against a planned rename migration, and set the convention a team follows so mappings do not drift model by model.

## Two names for every object A Prisma schema names things twice over: the **schema name** (the model, field or enum value as code sees it) and the **database name** (the table, column, enum type or stored value). By default they are the same string. Prisma's naming convention is singular PascalCase models and camelCase fields, while many existing databases use plural snake_case tables (`users`, `blog_posts`) and snake_case columns (`created_at`). The mapping attributes let both conventions coexist: | Attribute | Placed on | Maps to | Example | | --- | --- | --- | --- | | `@map("...")` | a field | a column (relational) or document field | `createdAt DateTime @map("created_at")` | | `@map("...")` | an enum value | the value stored in the database | `AUTHOR @map("author")` | | `@@map("...")` | a model block | a table or collection | `@@map("users")` | | `@@map("...")` | an enum block | the database enum type | `@@map("user_role")` | The attributes do not rename anything in the database. They tell Prisma what the database object is already called (or should be called, when Migrate creates it). ## Adopting a snake_case database The divergence shows up most clearly when a team brings an existing database under Prisma: 1. `prisma db pull` introspects the database and writes models named exactly like the tables: `model users { created_at DateTime ... }`. 2. Rename the model to `User` and add `@@map("users")`; rename each field to camelCase and add `@map("created_at")` and so on. 3. Run `prisma generate`. The client now exposes `prisma.user` with `createdAt`, and every query still targets `users.created_at`. 4. Check that the edited schema still describes the same database: a diff from the database to the schema should be empty, because only schema names changed. ```prisma enum Role { READER AUTHOR @map("author") ADMIN @@map("user_role") } model User { id Int @id @default(autoincrement()) email String @unique role Role @default(READER) createdAt DateTime @default(now()) @map("created_at") @@map("users") } ``` ## What code sees and what the database sees - **Client API:** only schema names. `prisma.user.findMany({ orderBy: { createdAt: "desc" } })` is valid; `created_at` is not a property of the client type. Compound selectors for `@@id` and `@@unique` are also built from field names, not column names. - **SQL:** only database names. The migration for the model above creates table `users`, column `created_at`, and an enum type `user_role` whose values are `'READER'`, `'author'` and `'ADMIN'`. - **Enums in TypeScript:** the generated constant keeps the schema names. In Prisma 7, `Role.AUTHOR` evaluates to `"AUTHOR"`, not `"author"`; the client translates to the stored value on the way to the database and back. Code that compares raw query results with `Role.AUTHOR` therefore sees `"author"` from the database and needs the mapping in mind. ## Constraint and index names follow the database Prisma Migrate names constraints and indexes it creates from the **table and column names**, not from the model and field names: - primary key: `{table}_pkey`, so `users_pkey` - unique constraint or index: `{table}_{columns}_key`, so `users_email_key` - non-unique index: `{table}_{columns}_idx` - foreign key: `{table}_{columns}_fkey` A legacy database whose constraints follow another convention keeps them by adding the `map:` argument on the key or index attribute, for example `@id(map: "pk_users")` or `@@index([email], map: "ix_users_email")`. Introspection writes `map:` only where a name differs from Prisma's convention. ## Changing a mapping later On a database Prisma Migrate already manages, a mapped name is part of the schema. Changing `@@map("users")` to `@@map("app_users")` makes Migrate generate `DROP TABLE "users"` followed by `CREATE TABLE "app_users"`; changing `@map("first_name")` to `@map("given_name")` generates `DROP COLUMN "first_name"` plus `ADD COLUMN "given_name"`. Neither is a rename, and on a populated table both lose data. Review the generated SQL before it is applied and replace the drop-and-create with an `ALTER TABLE ... RENAME` by hand; how that edited migration reaches production is covered with Prisma Migrate. The reverse case is harmless: renaming a model or field in the schema while keeping its `@map` or `@@map` value changes only the client API, and the database diff is empty.

  • In Prisma 7, why does renaming a field in the schema while keeping its @map produce an empty migration?
    Prisma Migrate compares database names. If `firstName String @map("first_name")` becomes `givenName String @map("first_name")`, the column is still `first_name`, so there is nothing to change in SQL; only the generated client property changes, and every call site that used `firstName` must be updated.
  • What does a raw SQL query return for an enum column whose values use @map in Prisma 7?
    The stored database values. With `AUTHOR @map("author")`, a raw query returns `"author"`, while the generated `Role.AUTHOR` constant is `"AUTHOR"`. Only Prisma Client's model queries translate between the two, so raw-query code must compare against the mapped strings or translate them itself.

A performer billed under a stage name: the programme and the audience use the stage name, while the contract and the bank use the legal name. @map and @@map record the legal name (the database's) beside the stage name your code calls, and renaming the act on the programme changes nothing at the bank.

  • @@map renames the existing table in the database when you run generate.
  • With @map in place, client queries use the column name, such as created_at.
  • A mapped enum value changes the generated TypeScript constant to the lowercase string.
  • Prisma names a mapped model's primary key after the model, such as User_pkey.
  • Changing @@map on a migrated model produces an ALTER TABLE RENAME automatically.