prisma / prisma/orm

`CREATE EXTENSION btree_gin;` + `ops: raw("text_ops")` leads to endless migrations (and operator class is removed in Introspection)

Open
#17,518 9 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug/2-confirmed kind/bug topic: index/gin topic: indexes topic: introspection topic: prisma migrate dev
Dominant language
TypeScript
Stars
47.6k
Forks
2.5k
Avg merge
21h 59m
Merged PRs (30d)
95

Description

(Originally reported by @uhyo in https://github.com/prisma/prisma/issues/16275#issuecomment-1402212976)

Currently, Prisma can not handle the operator classes added by extensions (see issue for support: https://github.com/prisma/prisma/issues/17516). This means to create an index using such an operator class in e.g. a Gin index, you have to use the raw("...") operator class escape hatch and then use migrate dev --create-only and then modify the migration file to make sure the extension is enabled:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model A {
  id   Int    @id @default(autoincrement())
  data String

  @@index([data(ops: raw("text_ops"))], type: Gin)
}

Then add the first line to the migration file before the correctly generated SQL:

CREATE EXTENSION btree_gin;
CREATE INDEX "A_data_idx" ON "A" USING GIN ("data" text_ops);

This migration can be applied with migrate deploy, but any future migrate dev run will again create a useless migration like this:

-- DropIndex
DROP INDEX "A_data_idx";

-- CreateIndex
CREATE INDEX "A_data_idx" ON "A" USING GIN ("data" text_ops);

(Note how the index definition is exactly the same as the original index definition that was already run)

Running Introspection will also remove the raw("text_ops") operator class from the original Prisma schema.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Prisma schema and SQL shown in the issue, then compare repeated migrate dev runs with migrate deploy and with introspection. Trace the migration diff and introspection paths that handle the Gin index and raw("text_ops") operator class. Done means the existing index is not recreated on later migrations and introspection preserves the operator class.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, typescript
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.