1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Override Table Name For Prisma Model as a Prisma TIL

This commit is contained in:
jbranchaud
2024-05-20 09:05:44 -05:00
parent 39614e975e
commit 84e2c9c6f4
2 changed files with 44 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
# Override Table Name For Prisma Model
When defining your Prisma schema, you'll add models to your
`prisma/schema.prisma` file that look something like this:
```
model Book {
id BigInt @id @default(autoincrement()) @db.BigInt
title String
author String
publication_year Int
created_at DateTime @default(now())
updated_at DateTime @updatedAt
}
```
The prisma client (ORM-layer) that gets generated will have a `Book` type and
you'll be able to reference the model to, for instance, create a record with
`prisma.book.create(...)`. Both of these things are derived from the model
name: `Book`.
The other thing that is derived from the model name is the name given to the
underlying database table. So you end up with a table called `Book`. You may,
however, prefer a table naming convention where this one would be named `books`
(snake_case and pluralized).
To achieve that, you have to manually override the table name with [the `@@map`
directive](https://www.prisma.io/docs/orm/reference/prisma-schema-reference#map-1).
Add it toward the bottom of the model like so:
```
model Book {
id BigInt @id @default(autoincrement()) @db.BigInt
title String
author String
publication_year Int
created_at DateTime @default(now())
updated_at DateTime @updatedAt
@@map("books")
}
```