Indices on Single Column Expressions
See original GitHub issueProblem
Commonly we define indices (and unique indices) that make use of expressions, most commonly LOWER(col)
and UPPER(col)
, though we are unable to express that in our schema.prisma
. Furthermore prisma actually tries to remove our indices containing expressions in every subsequent migration, causing us to manually update each migration (looks like this is already captured in https://github.com/prisma/prisma/issues/12914).
Edit: As of prisma 4.1, prisma migrate dev
no longer attempts to remove the existing index on those columns, but still tries to add the new index. This causes the newly added migration to fail.
An isolated reproducible example of this can be found here.
Suggested solution
model User {
id String @id
firstName String @map("first_name")
@@index([@lower(firstName)])
@@map("users")
}
to generate
CREATE INDEX "user_first_name_idx" ON "user"(LOWER("first_name"));
Ideally the solution would be able to be applied to multi column indices the same as single column indices
@@index([@lower(firstName), @lower(lastName)])
as well as unique indices.
@@unique([@lower(firstName), @lower(lastName)])
Alternatives
Adding a standard index @@index([firstName])
and then manually updating the generated migration to use an expression on the desired columns.
Issue Analytics
- State:
- Created a year ago
- Reactions:6
- Comments:6 (3 by maintainers)
Top GitHub Comments
Let’s close this then, and you make sure that the missing bits are mentioend over there via a comment please.
duplicate of https://github.com/prisma/prisma/issues/2504
When attempting to update to prisma 4.1 I followed the upgrade guide and ran
prisma db pull
. It attempted to delete my functional index from my prisma schema.If I add that index back to my schema and then run
prisma migrate dev
it now doesn’t attempt to delete the functional index, but does attempt to add a new one which fails. This is preventing me from upgrading to prisma 4.xWith the new
@@index([created_at(sort: Desc)])
syntax being added. Maybe something like@@index([author, name(lower: true)])
could make sense for this feature request?