Preserve database naming
A database table is named comments, but the schema should use the singular Comment model.
Prisma · Const
@@map maps a Prisma model to a differently named database table or MongoDB collection. It lets Prisma Client use a clean model name while preserving the database object's existing name.
Add @@map to a model to decouple its Prisma name from the underlying database object. Prisma Client uses the model name, while relational databases use the mapped table name and MongoDB uses the mapped collection name.
@@map("tableOrCollectionName")
@@map(name: "tableOrCollectionName")| Name | Type | Description |
|---|---|---|
name | String | Database table name for relational databases, or collection name for MongoDB; the page also allows an underlying enum name. |
No runtime value; this schema attribute controls the database object name used by Prisma ORM.
@@map connects a Prisma model to a differently named relational table or MongoDB collection.name label is optional, so named and positional syntax are equivalent.@@map, Prisma expects the model name to match the relational table or MongoDB collection name.@@map(name: "users") and @@map("users") mean the same thing.model Comment {
id Int @id @default(autoincrement())
text String
@@map("comments")
}This keeps the Prisma model name Comment while reading and writing the existing comments table.
A database table is named comments, but the schema should use the singular Comment model.
An existing database uses names that do not follow Prisma model naming conventions.
The Prisma Client API should use domain-friendly model names without changing stored database object names.
@@map do?@@map maps a Prisma model name to a different database table or MongoDB collection name. This lets Prisma Client expose a model such as Comment while the database object is named comments.
Follow-up: How would you map a field as well? Use field-level @map, for example text String @map("comment_text"); @@map applies to the model, whereas @map applies to a field or enum value.
@@map take?Its required name value is the database table name for relational databases or collection name for MongoDB. The attribute can use either @@map("users") or @@map(name: "users").
Follow-up: What happens if the attribute is omitted? Prisma uses the model name itself as the table or collection name.
@@map?Use @@map when the database uses naming conventions or legacy names that differ from the names preferred in the Prisma schema. Prisma Client then remains idiomatic without requiring the underlying database object to be renamed.
Follow-up: Can @@map rename an enum in the database? Yes; the schema reference also documents mapping an enum name to a different underlying database enum.
@map@@schemaprisma db pull