In Prisma 4.6.0, changing `onDelete: SetNull` to `Cascade` results in recreating foreign key
See original GitHub issueDiscussed 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:
- Created 10 months ago
- Comments:6 (4 by maintainers)
Top 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 >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
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!
4.6.1 also fixed the enum error for us. 4.6.0 caused the problem.