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.

Incorrect migration creates on sql server when index deleted

See original GitHub issue

Bug description

Initial prisma model contains unique keys:

model Logbook {    
  categoryId     String?         @db.UniqueIdentifier
  date           DateTime       @db.Date()
  @@unique([date, categoryId], name: "Date_Category_unique_constraint")
}

It creates such sql statement: -- CreateIndex CREATE UNIQUE NONCLUSTERED INDEX [Logbook_date_categoryId_key] ON [dbo].[Logbook]([date], [categoryId]); When I delete unique constraint from prisma’s schema model

model Logbook {    
  categoryId     String?         @db.UniqueIdentifier
  date           DateTime       @db.Date()
}

it creates migration with such statement: -- DropIndex ALTER TABLE [dbo].[Logbook] DROP CONSTRAINT [Logbook_date_categoryId_key]; Applying this migration throws an error: “Database error: ‘Logbook_date_categoryId_key’ is not a constraint.”

How to reproduce

  1. Add unique keys constraint for your model
  2. Apply migration
  3. Delete constraint
  4. Apply migration

Expected behavior

Should call DROP INDEX, not DROP CONSTRAINT;

Prisma information

model Logbook {    
  categoryId     String?         @db.UniqueIdentifier
  date           DateTime       @db.Date()
  @@unique([date, categoryId], name: "Date_Category_unique_constraint")
}

Environment & setup

  • OS: Windows
  • Database: Sql Server
  • Node.js version: 16.15.1

Prisma Version

3.15.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pimeyscommented, Jun 29, 2022

I made a fix, this should work with all Prisma cases. Read the details from the PR above.

Now you should manually edit the drop to your migration for it to work. In the future, if you alter a table with @unique, we add a constraint instead of an index. This can then be dropped with DROP CONSTRAINT.

Definitely interesting how SQL Server makes a distinction here, and why you can’t drop a unique index with DROP CONSTRAINT.

1reaction
pimeyscommented, Jun 29, 2022

Ah, found it! So you must first create a table without a unique constraint, then add it and then remove it again. Now it fails.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql server - Database events that will cause deletion of index
It depends on the method you're using to alter the column. We'll start with a simple table: CREATE TABLE dbo.Customers (ID INT IDENTITY(1,1) ......
Read more >
Database Engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages for SQL Server database engine events.
Read more >
Poor database indexing - a SQL query performance killer
Any SQL Server table configuration where performance suffers due to excessive, improper, or missing indexes is considered to be poor indexing.
Read more >
Indexing for Deletes - Brent Ozar Unlimited®
SQL Server says “OK, let's make sure that we can actually delete this row, what else depends on it?” SQL Server will check...
Read more >
Entity Framework Core Migrations
Sometimes, you may want to execute custom SQL at the same time as the migration is applied to the database. You may need...
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