diff --git a/content/200-orm/100-prisma-schema/20-data-model/30-indexes.mdx b/content/200-orm/100-prisma-schema/20-data-model/30-indexes.mdx index 958ad1c732..66ac00544d 100644 --- a/content/200-orm/100-prisma-schema/20-data-model/30-indexes.mdx +++ b/content/200-orm/100-prisma-schema/20-data-model/30-indexes.mdx @@ -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` ( @@ -106,7 +110,7 @@ CREATE TABLE `Unique` ( ) ``` -is now introspected as +would be introspected as ```prisma file=schema.prisma showLineNumbers model Unique { @@ -114,6 +118,13 @@ model Unique { } ``` +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