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.

In Prisma 4.6.0, changing `onDelete: SetNull` to `Cascade` results in recreating foreign key

See original GitHub issue

Discussed in https://github.com/prisma/prisma/discussions/16223

<div type='discussions-op-text'>

Originally posted by jameyhart November 10, 2022 @janpio After upgrading to 4.6.0, we started reciving the following error when migrating our database:

Error: Schema validation error - Error (query-engine-node-api library)
Error code: P1012
error: Error parsing attribute "@relation": The `onDelete` referential action of a relation must not be set to `SetNull` when a referenced field is required.
Either choose another referential action, or make the referenced fields optional.

  -->  schema.prisma:126
   |
125 |     
126 |     survey    Survey   @relation(fields: [surveyId], references: [id], onDelete: SetNull)
127 |
   |

To rectify this, we changed the onDelete from SetNull to Cascade but it then cerates a new migration that drops the foreign key and recreates it. Is this the expected behaviour?

Our current database model:

model Survey {
    id                      String                    @id @default(cuid())
    name                    String
    survey                  Json
    reportSurvey            SurveyReport[]
    ...
}

model SurveyReport {
    id                String       @id @default(cuid())
    surveyId          String
    survey            Survey       @relation(fields: [surveyId], references: [id], onDelete: SetNull)
    reportType        SurveyReportType
    ...
}

The output from the new migration:

-- DropForeignKey
ALTER TABLE "SurveyReport" DROP CONSTRAINT "SurveyReport_surveyId_fkey";

-- AddForeignKey
ALTER TABLE "SurveyReport" ADD CONSTRAINT "SurveyReport_surveyId_fkey" FOREIGN KEY ("surveyId") REFERENCES "Survey"("id") ON DELETE CASCADE ON UPDATE CASCADE;
</div>

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
jameyhartcommented, Nov 11, 2022

Hi both, thanks for the speedy reply!

So, upgrading to v4.6.1 fixed the bug we were experiecing with enums. Yes, the schema was written by hand. The new validation helped us highlight an issue/oversight from us in the schema, i.e. we were trying to set the Survey field to NULL if a survey was deleted, however the field cannot be NULL as it’s required. So that’s appreciated.

Also, yes - everything you said makes sense. I’ve discussed with the team and we’re happy for you to close this issue and we’ll review both our Prisma versions and our database models going forward.

Thanks so much!

3reactions
elie222commented, Nov 16, 2022

4.6.1 also fixed the enum error for us. 4.6.0 caused the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

In Prisma 4.6.0, changing onDelete: SetNull to Cascade ...
To rectify this, we changed the onDelete from SetNull to Cascade but it then cerates a new migration that drops the foreign key...
Read more >
Referential actions - Prisma
Referential actions let you define the update and delete behavior of related models on the database level.
Read more >
SQL - Foreign Key - On Delete Cascade - On Delete Set Null
In this session we will understand the application of On Delete Cascade and On Delete Set Null clauses while creating Foreign Keys in...
Read more >
SQL Server: Foreign Keys with set null on delete
A foreign key with "set null on delete" means that if a record in the parent table is deleted, then the corresponding records...
Read more >
Is it a good or bad idea to use "ON UPDATE CASCADE ON ...
In theory your primary key should be static so changes that need cascading shouldn't need to happen. Perhaps it was added as a...
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