Skip to content

Commit 96f5587

Browse files
Add documentation for map argument on Prisma indexes and constraints (#7397)
1 parent b698252 commit 96f5587

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

content/200-orm/100-prisma-schema/20-data-model/30-indexes.mdx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ You can configure indexes, unique constraints, and primary key constraints with
4242
- Available on the `@id`, `@@id`, `@unique`, `@@unique` and `@@index` attributes
4343
- SQL Server only
4444

45+
- The [`map` argument](#configuring-the-name-of-indexes-with-map) allows you to specify a custom name for the index or constraint in the underlying database
46+
- Available on the `@id`, `@@id`, `@unique`, `@@unique` and `@@index` attributes
47+
- Supported in all databases
48+
4549
See the linked sections for details of which version each feature was first introduced in.
4650

4751
### Configuring the length of indexes with `length` (MySQL)
@@ -458,6 +462,54 @@ The default value of `clustered` for each attribute is as follows:
458462

459463
A table can have at most one clustered index.
460464

465+
### Configuring the name of indexes with `map`
466+
467+
The `map` argument allows you to specify a custom name for the index or constraint in the underlying database. This is useful when you want to use a specific naming convention or when the auto-generated name doesn't meet your requirements.
468+
469+
The `map` argument is available on the `@id`, `@@id`, `@unique`, `@@unique` and `@@index` attributes.
470+
471+
As an example, the following model configures a custom name for the index on the `title` field:
472+
473+
```prisma file=schema.prisma showLineNumbers
474+
model Post {
475+
id Int @id
476+
title String
477+
478+
@@index([title], map: "my_custom_index_name")
479+
}
480+
```
481+
482+
This translates to the following SQL command (PostgreSQL example):
483+
484+
```sql
485+
CREATE INDEX "my_custom_index_name" ON "Post" ("title");
486+
```
487+
488+
Without the `map` argument, Prisma would generate a default name like `Post_title_idx`.
489+
490+
The `map` argument can also be used on unique constraints:
491+
492+
```prisma file=schema.prisma showLineNumbers
493+
model User {
494+
id Int @id
495+
email String @unique(map: "unique_user_email")
496+
}
497+
```
498+
499+
And on composite indexes and constraints:
500+
501+
```prisma file=schema.prisma showLineNumbers
502+
model Post {
503+
id Int @id
504+
title String
505+
author String
506+
createdAt DateTime
507+
508+
@@index([author, createdAt], map: "posts_author_date_idx")
509+
@@unique([title, author], map: "posts_title_author_unique")
510+
}
511+
```
512+
461513
### Upgrading from previous versions
462514

463515
:::warning

0 commit comments

Comments
 (0)