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.

Postgres: Prisma push/migrate with custom unique index fails

See original GitHub issue

Bug description

When there is custom unique index (altered migration file generated by Prisma), Prisma cannot see it and generates the same index with default unique parameters.

Custom index:

CREATE UNIQUE INDEX "Mixture_aromaId_boosterId_baseId_nicotine_key" ON "Mixture"(hash_record_extended(("aromaId", "boosterId", "baseId", "nicotine"), 0));

Prisma wants/generates:

CREATE UNIQUE INDEX "Mixture_aromaId_boosterId_baseId_nicotine_key" ON "Mixture"(("aromaId", "boosterId", "baseId", "nicotine"));
model Mixture {
  id              String        @id @default(cuid())
  // total size of a bottle
  volume          Decimal       @db.Decimal(10, 2)
  // amount of liquid in a bottle
  content         Decimal       @db.Decimal(10, 2)
  // amount of available space to be filled
  available       Decimal       @db.Decimal(10, 2)
  // volume - content => amount of extra or missing liquid
  diff            Decimal       @db.Decimal(10, 2)
  // amount of nicotine (mg/ml)
  nicotine        Decimal       @db.Decimal(10, 2)
  nicotineToRound Int
  // vg/pg ratio
  vg              Decimal       @db.Decimal(10, 2)
  pg              Decimal       @db.Decimal(10, 2)
  // vg/pg in ml
  vgToMl          Decimal       @db.Decimal(10, 2)
  // rounded VG ratio (for example 70 instead of 76)
  vgToRound       Int
  pgToMl          Decimal       @db.Decimal(10, 2)
  // rounded PG ratio (for example 70 instead of 76)
  pgToRound       Int
  aromaId         String
  aroma           Aroma         @relation(fields: [aromaId], references: [id], onDelete: Cascade)
  boosterId       String?
  booster         Booster?      @relation(fields: [boosterId], references: [id], onDelete: Cascade)
  boosterCount    Int
  baseId          String?
  base            Base?         @relation(fields: [baseId], references: [id], onDelete: Cascade)
  baseMl          Decimal       @db.Decimal(10, 2)
  error           MixtureError?

  Liquid Liquid[]

  @@unique([aromaId, boosterId, baseId, nicotine])
}

Given error is not showstopper, but makes DX a much harder as it fails on push.

How to reproduce

  • (on Postgres)
  • create model with multiple unique columns
  • alter generated .sql file slightly (like example provided)
  • reset database to execute updated migrations (what to say, you know the best what to do 😃 )
  • basically in any time execute prisma push; it will try to add already created unique index

Expected behavior

Prisma detects uniqe keys at least by name and do not try to add it again with it’s (prisma’s) defaults.

Prisma information

"@prisma/client": "^3.13.0",

Environment & setup

  • OS: Docker (alpine linux)
  • Database: Postgres
  • Node.js version: v16.13.0

Prisma Version

prisma                  : 3.13.0
@prisma/client          : 3.13.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine efdf9b1183dddfd4258cd181a72125755215ab7b (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli efdf9b1183dddfd4258cd181a72125755215ab7b (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core efdf9b1183dddfd4258cd181a72125755215ab7b (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt efdf9b1183dddfd4258cd181a72125755215ab7b (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : efdf9b1183dddfd4258cd181a72125755215ab7b
Studio                  : 0.459.0

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
marek-hanzalcommented, May 5, 2022

I understand that, the problem is Prisma trying to create already existing index. I was expecting Prisma gets indexes by name (is that possible?) and does not create them if they exists.

For the hash: on new Postgres it’s possible to create unique index on fields including null values, thus this method (hash_record_extended). It’s quite edge case, but it would be nice to prevent creation of already existing indexes.

Source of the finding: https://stackoverflow.com/questions/69330676/unique-index-with-a-lots-of-columns

0reactions
janpiocommented, Dec 7, 2022

The feature “expression indexes” is tracked here: https://github.com/prisma/prisma/issues/2504 The bug is fundamentally caused by us not supporting that (yet). Upvotes on that issue would be helpful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Indexes - Prisma
Prisma allows configuration of database indexes, unique constraints and primary key constraints. This is in General Availability in versions 4.0.0 and later ...
Read more >
Prisma schema API (Reference)
Specify a PostgreSQL data source via an environment variable. In this example, the target database is available with the following credentials: User: johndoe ......
Read more >
Evolve your schema - Prisma
In this section, you will evolve your Prisma schema and then generate and apply the migration to your database with prisma migrate dev...
Read more >
Prisma Migrate | Database, Schema, SQL Migration Tool
Prisma Migrate is a database migration tool available via the Prisma CLI that integrates with Prisma schema for data modeling.
Read more >
Schema Incompatibilities | PostgreSQL - Prisma
Default values aren't represented in database. Problem. When adding the @default directive in a Prisma 1 datamodel, ...
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