Incorrect migration creates on sql server when index deleted
See original GitHub issueBug 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
- Add unique keys constraint for your model
- Apply migration
- Delete constraint
- 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:
- Created a year ago
- Comments:6 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 withDROP CONSTRAINT
.Definitely interesting how SQL Server makes a distinction here, and why you can’t drop a unique index with
DROP CONSTRAINT
.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.