'Use of @map and @@map for Prisma schema
I am new to Prisma and have been wondering what is the use of @map and @@map for Prisma schema? I have looked at their website: https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#map but still don't fully get the purpose of @map and @@map.
From what I can comprehend, if I have something like lastName String @map("last_name") @db.VarChar(256) it maps "last_name" to lastname? But I am not too sure when I will need this.
Thank you! Appreciate any help.
Solution 1:[1]
@map can be used to assign a different name to your columns names, for example if the column name for a table in your database is userLastName but you want to assign a different name (user_last_name) and access it with a different name in your generated PrismaClient you can use @map attribute for it.
model User {
id Int @id @default(autoincrement())
userLastName String @map("user_last_name")
}
@mapdoes not rename the columns / fields in the database@mapdoes change the field names in the generated prisma client
On the other hand @@map is used to assign a different name to a model and not a particular field. So for example if a table name is UserDetails and you want to access it as user_details in the generated client you can use @@map attribute.
model UserDetails {
id Int @id @default(autoincrement())
name String
@@map("users_details")
}
Solution 2:[2]
Maps a field name or enum value from the Prisma schema to a column or document field with a different name in the database
A use case could be people integrating existing database with prisma and want to use different naming conventions between database & prisma client.
For example, one might use snakecase for database column but want to use camelcase for prisma client, then they will do:
model User {
createdAt String @map("created_at")
}
Same thing with @@map, but just for table name.
Detailed guide from prisma:
https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/use-custom-model-and-field-names
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nurul Sundarani |
| Solution 2 | Ironolife |
