Failure creating a migration with MSSQL: `Introducing FOREIGN KEY constraint '...' on table '...' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.`
See original GitHub issueBug description
I attempted to use Prisma Migrate (2.17.0) to create a migration from the Prisma schema below and got the following error:
Error: Database error: Error querying the database: 'Introducing FOREIGN KEY constraint 'FK__Comment__postId' on table 'Comment' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.' on server 46f430d4d1fc executing on line 57 (code: 1785, state: 0, class: 16)
0: migration_core::api::ApplyMigrations
at migration-engine/core/src/api.rs:51
It seems that the combination of defaulting to cascading deletes and a circular relation (between User : Post : Comment : User) causes the SQL to be invalid.
How to reproduce
- Create the Prisma schema
- Run
npx prisma migrate dev --preview-featureto generate and apply the migration
This is where the error shows up.
I was able to resolve this by updating the FK__Comment__postId constraint in the migration SQ.
-- CreateTable
CREATE TABLE [dbo].[User] (
[id] INT NOT NULL IDENTITY(1,1),
[createdAt] DATETIME2 NOT NULL CONSTRAINT [DF__User__createdAt] DEFAULT CURRENT_TIMESTAMP,
[email] NVARCHAR(1000) NOT NULL,
[name] NVARCHAR(1000),
CONSTRAINT [PK__User__id] PRIMARY KEY ([id]),
CONSTRAINT [User_email_unique] UNIQUE ([email])
);
-- CreateTable
CREATE TABLE [dbo].[Post] (
[id] INT NOT NULL IDENTITY(1,1),
[createdAt] DATETIME2 NOT NULL CONSTRAINT [DF__Post__createdAt] DEFAULT CURRENT_TIMESTAMP,
[title] NVARCHAR(1000) NOT NULL,
[content] NVARCHAR(1000),
[published] BIT NOT NULL CONSTRAINT [DF__Post__published] DEFAULT 0,
[authorId] INT NOT NULL,
CONSTRAINT [PK__Post__id] PRIMARY KEY ([id])
);
-- CreateTable
CREATE TABLE [dbo].[Comment] (
[id] INT NOT NULL IDENTITY(1,1),
[createdAt] DATETIME2 NOT NULL CONSTRAINT [DF__Comment__createdAt] DEFAULT CURRENT_TIMESTAMP,
[comment] NVARCHAR(1000) NOT NULL,
[writtenById] INT NOT NULL,
[postId] INT NOT NULL,
CONSTRAINT [PK__Comment__id] PRIMARY KEY ([id])
);
-- CreateTable
CREATE TABLE [dbo].[Tag] (
[id] INT NOT NULL IDENTITY(1,1),
[tag] NVARCHAR(1000) NOT NULL,
CONSTRAINT [PK__Tag__id] PRIMARY KEY ([id]),
CONSTRAINT [Tag_tag_unique] UNIQUE ([tag])
);
-- CreateTable
CREATE TABLE [dbo].[_TagToPost] (
[A] INT NOT NULL,
[B] INT NOT NULL,
CONSTRAINT [_TagToPost_AB_unique] UNIQUE ([A],[B])
);
-- CreateIndex
CREATE INDEX [_TagToPost_B_index] ON [dbo].[_TagToPost]([B]);
-- AddForeignKey
ALTER TABLE [dbo].[Post] ADD CONSTRAINT [FK__Post__authorId] FOREIGN KEY ([authorId]) REFERENCES [dbo].[User]([id]) ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE [dbo].[Comment] ADD CONSTRAINT [FK__Comment__writtenById] FOREIGN KEY ([writtenById]) REFERENCES [dbo].[User]([id]) ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
-- I changed this one from "ON DELETE CASCADE ON UPDATE CASCADE" to "ON DELETE NO ACTION ON UPDATE NO ACTION" which resolved it
ALTER TABLE [dbo].[Comment] ADD CONSTRAINT [FK__Comment__postId] FOREIGN KEY ([postId]) REFERENCES [dbo].[Post]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;
-- AddForeignKey
ALTER TABLE [dbo].[_TagToPost] ADD CONSTRAINT [FK___TagToPost__A] FOREIGN KEY ([A]) REFERENCES [dbo].[Post]([id]) ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE [dbo].[_TagToPost] ADD CONSTRAINT [FK___TagToPost__B] FOREIGN KEY ([B]) REFERENCES [dbo].[Tag]([id]) ON DELETE CASCADE ON UPDATE CASCADE;
Expected behavior
It should generate an executable migration.
Prisma information
Prisma schema:
generator client {
provider = "prisma-client-js"
previewFeatures = ["microsoftSqlServer"]
}
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
comments Comment[]
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
title String
content String?
published Boolean @default(false)
authorId Int
author User @relation(fields: [authorId], references: [id])
comments Comment[]
tags Tag[] @relation("TagToPost")
}
model Comment {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
comment String
writtenById Int
postId Int
writtenBy User @relation(fields: [writtenById], references: [id])
post Post @relation(fields: [postId], references: [id])
}
model Tag {
id Int @id @default(autoincrement())
tag String @unique
posts Post[] @relation("TagToPost")
}
Environment & setup
- OS: Mac OS
- Database: Microsoft SQL Server (Docker image:
mcr.microsoft.com/mssql/server:2019-latest) - Node.js version: v14.15.0
- Prisma version:
➜ sql-server git:(latest) npx prisma version
Environment variables loaded from prisma/.env
prisma : 2.17.0
@prisma/client : 2.17.0
Current platform : darwin
Query Engine : query-engine 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/prisma/node_modules/@prisma/engines/query-engine-darwin)
Migration Engine : migration-engine-cli 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/prisma/node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/prisma/node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/prisma/node_modules/@prisma/engines/prisma-fmt-darwin)
Studio : 0.353.0
Preview Features : microsoftSqlServer
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9 (8 by maintainers)
Top Results From Across the Web
Foreign key constraint may cause cycles or multiple cascade ...
I get the error: Introducing FOREIGN KEY constraint 'FK74988DB24B3C886' on table 'Employee' may cause cycles or multiple cascade paths. Specify ...
Read more >MSSQLSERVER_1785 - SQL Server - Microsoft Learn
ls' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY...
Read more >Fix: Introducing foreign key constraint on table may ... - YouTube
Fix: Introducing foreign key constraint on table may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO...
Read more >Solving the SQL Server Multiple Cascade Path Issue with a ...
I am trying to use the ON DELETE CASCADE option when creating a foreign key on my database, but I get the following...
Read more >Introducing FOREIGN KEY constraint ... - Laracasts
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY ... on table 'my_l ist_user' may cause cycles...
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 Free
Top 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

We are doing it the right way and will add detection of cyclic referential actions via: https://github.com/prisma/prisma/issues/4580 This will be added while SQL Server is still in preview.
The question if we wrap migrations in transactions is handled separately: https://github.com/prisma/prisma/issues/7641
This still reproducible in 2.21.0-dev.38