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.

Unable to run `prisma migrate` with both `microsoftSqlServer` and `referentialActions` preview features enabled

See original GitHub issue

Bug description

Using SQL server (preview) as my database, i create a model with a self relation. This gives me the expected error that i have cyclic relations. To fix this i enable the referential actions preview feature and add onDelete: NoAction, onUpdate: NoAction to one of my relations.

I then try and run an initial migration, but i get an error from sql server instead and am unable to do the migration.

Error: P3018

A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve    

Migration name: 20210816094950_init

Database error code: 1785

Database error:
Introducing FOREIGN KEY constraint 'FK__Chicken__chickenId' on table 'Chicken' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.   

How to reproduce

This repo and its read me have the steps to reproduce: https://github.com/molebox/cylic-ref-action-bug

Expected behavior

I should be able to run a successful migration once the cyclic relations issue is fixed using referential actions

Prisma information


datasource db {
  provider = "sqlserver"
  url      = env("DATABASE_URL")
}

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["microsoftSqlServer", "referentialActions"]
}

model Chicken {
  id        Int      @id @default(autoincrement())
  cock      Chicken? @relation(name: "a_self_relation", onDelete: NoAction, onUpdate: NoAction)
  hen       Chicken? @relation(name: "a_self_relation", fields: [chickenId], references: [id])
  chickenId Int?
}

Environment & setup

  • OS: Windows 10
  • Database: Microsoft SQL Server
  • Node.js version: v14.15.1

Prisma Version

prisma               : 2.29.1
@prisma/client       : Not found
Current platform     : windows
Query Engine         : query-engine 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine     : migration-engine-cli 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary        : prisma-fmt 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : 1be4cd60b89afa04b192acb1ef47758a39810f3a
Studio               : 0.419.0
Preview Features     : microsoftSqlServer, referentialActions

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
pantharshit00commented, Aug 16, 2021

Thanks, I can reproduce this with 2.29.1

When you try to migrate the schema, it fails when the cycle detection error but it saves the migration to file system and to the _prisma_migrations table.

After this when you run migrate again, you will get New migrations cannot be applied before the error is recovered from

image

0reactions
Jolg42commented, Aug 19, 2021

https://github.com/prisma/prisma-engines/pull/2168 adds an error like this below, nice!

Error parsing attribute "@relation": The relation field `cock` on Model `Chicken` must not specify the `onDelete` or `onUpdate` argument in the @relation attribute.
You must only specify it on the opposite field `hen` on model `Chicken`.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Failure creating a migration with MSSQL: `Introducing ... - GitHub
Create the Prisma schema; Run npx prisma migrate dev --preview-feature to ... This should now work with the referentialActions preview feature enabled.
Read more >
Referential actions - Prisma
After upgrading and enabling the referential actions preview feature, Prisma no longer performs runtime checks. You can instead specify a custom referential ...
Read more >
Migration troubleshooting in development - Prisma
This guide describes how to resolve issues with Prisma Migrate in a development environment, which often involves resetting your database.
Read more >
Manage relations between records with relation modes in ...
When you use the Prisma Client with the prisma relation mode enabled, the behavior of queries is identical or similar, but referential actions...
Read more >
Special rules for referential actions in SQL Server and MongoDB
If the referential actions on the foreign key are set to something other than NO ACTION (or NoAction if Prisma is managing referential...
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