-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
relationMode = prisma
: Error validating: A self-relation must have onDelete
and onUpdate
referential actions set to NoAction
#17649
Comments
I can indeed reproduce your observation:
Investigating internally now if this is maybe a mistake that this is enabled for Thanks for the issue! |
@jkomyno did some sleuthing, and I am here with the simple job to report his findings: This is currently working as designed, although we are lacking the documentation for it. #9931 was an issue triggered by a user having a self relation, and it then crashing Prisma Client. This was fixed by expanding our SQL Server and MongoDB cyclic ref actions detections to these cases as well in this PR: prisma/prisma-engines#2415 Here is the explanation why this is needed:
And the "workaround":
Would this work for you @baptisteArno? |
Yes, totally fine! Thank you for the investigation 👌 |
We'll still keep this issue open as that is indeed unexpected, and we at least should make sure to document this. |
But
I think what I need is |
Hi @Songkeys, wouldn't onUpdate/onDelete |
Hi @jkomyno , I explained here. |
Thanks for the link to the comment @Songkeys. |
Also, after taking another look, I don't understand the solution above even for db like mysql:
In this PR:
How does the |
Not sure if this has something to do with this issue, but I'm getting an issue when trying to delete a self-relation. When I try deleting all nested children in a 1-M self-relation, I get the error below. However, using a raw query works. Schema: model Topic {
id Int @id @default(autoincrement())
parentTopicId Int?
parentTopic Topic? @relation("Subtopic", fields: [parentTopicId], references: [id], onDelete: NoAction, onUpdate: NoAction)
subTopics Topic[] @relation("Subtopic")
} Prisma delete query (trying to delete all subtopics): await prisma.topic.deleteMany({
where: { NOT: { parentTopicId: null } },
}); Error message:
Solution (raw query): await prisma.$queryRaw`
DELETE FROM topics T
WHERE T. parentTopicId IS NOT NULL
`; Is there any current solution to this without using a raw query? |
I tried the NoAction for onDelete and for onUpdate but I'm still getting this error. (Provider MongoDB)
My Model
I would love if someone could help me out. |
Now when I run
|
Prisma seems weird, I suspect that the line of code is correct on my part but it throws error
|
Hello, I have almost the same problem. Have you managed to solve it somehow?
|
This problem makes it pretty much impossible to work with self-relating models. I don't understand how come NoAction works like Restrict: model GroupActivity {
id Int @id @default(autoincrement())
rootComment Int?
RootComment GroupActivity? @relation("NestedReplies", fields: [rootComment], references: [id], onDelete: NoAction, onUpdate: NoAction)
NestedReplies GroupActivity[] @relation("NestedReplies")
groupId String
// this can't be Cascade because we then hit: The change you are trying to make would violate the required relation 'NestedReplies' between the `GroupActivity` and `GroupActivity` models.
Group OrganisationGroup @relation(fields: [groupId], references: [id], onDelete: NoAction)
@@index(groupId)
@@index(rootComment)
} If I was just deleting GroupActivities it would've been fine because I can just drop to raw queries, but if I come from a long chain of cascades through the group relation, I would need to write a bunch of extra code to clean up this table before we delete any of the parent resources. Not only that but all of this should also be done as a transaction just in case part of it fails, so we don't end up in a flaky state of half-deleted chained resources. Anyone with ideas on how I should go about it? The solution I went ahead with looks something like this. Looks terrible but seems like the only escape hatch for now. const relatedGroups = getRelatedGroupsDependingOnWhatGetsDeleted(idToDelete);
await db.$transaction([
...deleteGroupActivitiesIn(relatedGroups),
db.whateverTopLevelResource.delete({
where: { id: idToDelete},
}),
]);
const deleteGroupActivitiesIn = (groupIds: string[]) => {
// we need to return array otherwise we can't call .join with an empty array
// it throws an error... but we can't just return undefined either because
// it is supposed to be used inside $transaction which expects a PrismaPromise
return groupIds.length === 0 ? [] : [
db.$queryRaw`
DELETE FROM GroupActivity WHERE groupId IN (${Prisma.join(groupIds)})
`,
];
}; |
@ivstiv I agree, I have similar schema with nested folders. Is this still being looked at? |
Worked with me using mongoDB as well |
Is this issue still being worked on? |
Bug description
With mysql and relationMode set to
prisma
, I'm getting this lint error:Even though that should only be the case if it uses MongoDB or SQL Server providers as explained here?
Prisma information
The text was updated successfully, but these errors were encountered: