SQL Server - Migrate: Unable to alter primary key after creating the table
Author: Faheem-makerCreated Jun 26, 2023Updated Sep 29, 2026
Labelsbug/2-confirmedkind/bugtopic: migratetopic: sql servertopic: broken migrations
Bug description
I have a model in prisma.schema that looks a little like this:
model PayrollCheque {
parentChequeId Int @unique @id
parentCheque Cheque @relation(fields: [parentChequeId], references: [id])
// Other fields
}I realized that I want multiple payrolls for a single parent cheque, so I changed my schema to have a different primary key.
model PayrollCheque {
id Int @id @default(autoincrement())
parentChequeId Int
parentCheque Cheque @relation(fields: [parentChequeId], references: [id])
// Other fields
}I attempted to create a migration and deploy it, only to get the following error:
Database error:
Column name 'id' does not exist in the target table or view._I checked the migration and Prisma was adding a primary key constraint before creating the ID column. Here is a little representation of what was being generated:
ALTER TABLE [dbo].[PayrollCheque] ADD CONSTRAINT PayrollCheque_pkey PRIMARY KEY CLUSTERED ([id]);
ALTER TABLE [dbo].[PayrollCheque] ADD [id] INT NOT NULL IDENTITY(1,1);How to reproduce
Expected behavior
ID should be added before the primary key is created.
Prisma information
I can't include my schema.prisma as it contains confidential information.
Environment & setup
- OS: Windows 10
- Database: Microsoft SQL Server 2016
- Node.js version: v18.14.1
Prisma Version
Prisma v4.15.0Source: prisma/prisma