Skip to content

Resolves #6839 #6925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions content/200-orm/100-prisma-schema/20-data-model/30-indexes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ A similar syntax can be used for the `@@unique` and `@@index` attributes.

### Configuring the index sort order with `sort`

The `sort` argument is available for all databases supported by Prisma ORM. It allows you to specify the order that the entries of the index or constraint are stored in the database. This can have an effect on whether the database is able to use an index for specific queries.
The `sort` argument allows you to specify the order that the entries of the index or constraint are stored in the database. This can have an effect on whether the database is able to use an index for specific queries. The behavior and support varies by database:

The `sort` argument is available for all databases on `@unique`, `@@unique` and `@@index`. Additionally, SQL Server also allows it on `@id` and `@@id`. It is generally available in versions 4.0.0 and later, and available as part of the `extendedIndexes` preview feature in versions 3.5.0 and later.
- In MySQL/MariaDB, you can specify sort order (`ASC`/`DESC`) directly in unique constraints and indexes
- In PostgreSQL, sort order can only be specified on indexes, not on unique constraints
- In SQL Server, sort order is supported on all constraints and indexes including `@id` and `@@id`

As an example, the following table
The `sort` argument is generally available in versions 4.0.0 and later, and available as part of the `extendedIndexes` preview feature in versions 3.5.0 and later.

For example, in MySQL/MariaDB, the following table using a descending unique constraint:

```sql
CREATE TABLE `Unique` (
Expand All @@ -106,14 +110,21 @@ CREATE TABLE `Unique` (
)
```

is now introspected as
would be introspected as

```prisma file=schema.prisma showLineNumbers
model Unique {
unique Int @unique(sort: Desc)
}
```

Note that in PostgreSQL, while you cannot specify sort order on unique constraints directly, you can create a unique index with a sort order that will enforce uniqueness:

```sql
-- PostgreSQL approach
CREATE UNIQUE INDEX "unique_index_desc" ON "Unique" ("unique" DESC);
```

The `sort` argument can also be used on compound indexes:

```prisma file=schema.prisma showLineNumbers
Expand Down