question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Indices on Single Column Expressions

See original GitHub issue

Problem

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:closed
  • Created a year ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
janpiocommented, Nov 16, 2022

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

1reaction
drewcorlin1commented, Jul 21, 2022

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.x

With 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expression Indexes | CockroachDB Docs
Expression indexes apply a scalar or functional expression to one or more columns.
Read more >
Documentation: 15: 11.7. Indexes on Expressions - PostgreSQL
An index column need not be just a column of the underlying table, but can be a function or scalar expression computed from...
Read more >
Indexes On Expressions - SQLite
Use a CREATE INDEX statement to create a new index on one or more expressions just like you would to create an index...
Read more >
Expression-based indexes - IBM
Simple index keys consist of a concatenation of one or more specified table columns. Compared to simple indexes, the index key values of...
Read more >
How to create indexes on SQL Server computed columns
The Computed Column can be referenced by a SELECT statement columns list, WHERE or ORDER BY clauses, but it can't be used in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found