Error when trying to delete a node with nested fields using onDelete: CASCADE,

prisma

#1

I’m currently encountering an error when trying to delete a node. The node has a relation with another node which is one of its fields. This other node has a relation with yet another node through one of its fields. See the image below.


Hence, when I try to delete a Hunt, since onDelete is set to Cascade, it should the associated Phase. But here’s when the problem arises. When it tries to delete the associated Phase, the relation between the Phase and its Interaction doesn’t allow the Phase to be deleted arising to the error:
“The change you are trying to make would violate the required relation ‘PhaseInteraction’ between Interaction and Phase”.

This is probably because the onDelete for the Phase isn’t triggered due to the fact that it was deleted by the onDelete for the Hunt, and not from a normal prisma delete.

Is there any way I can bypass this?


#2

Hey, from a first look I would say this should be working as you expected it to do. I’ll try to reproduce it tomorrow and check whether this is a bug.


#3

Hi do4gr,
Currently, I am not able to delete a hunt node even though I have onDelete: CASCADE. If I try, I get the error “The change you are trying to make would violate the required relation ‘PhaseInteraction’ between Interaction and Phase”. So to delete a hunt, I’d have to delete the answer first, then delete the interaction, then the phase which kind of ruins the point of a onDelete: CASCADE. :frowning:


#4

Yeah, I think I understood your issue and will look into it. Could you please post your datamodel in text form? Then it is a lot easier for me to reproduce the issue.


#5

Here it is in text form.

type Hunt {
    id: ID! @unique
    name: String!
    numberOfPhases: Int!
    phases: [Phase!]! @relation(name: "HuntPhase", onDelete: CASCADE)
}

type Phase{
    hunt: Hunt! @relation(name: "HuntPhase", onDelete: SET_NULL)
    info: String!
    items: [Item]
    locationType: _LocationType!
    location: String!
    interactions: Interaction! @relation(name: "PhaseInteraction", onDelete: CASCADE)
}

type Interaction{
    phase: Phase! @relation(name: "PhaseInteraction", onDelete: SET_NULL)
    type: _InteractionType!
    answer: Answer @relation(name: "InteractionAnswer", onDelete: CASCADE)
}

type Answer{
    interaction: Interaction! @relation(name: "InteractionAnswer", onDelete: SET_NULL)
    type: _AnswerType!
    relation: _Relation
    answer: String
    answerTag: _Trivia
}

Thanks!


#6

Which connector are you using and which version of Prisma?


#7

Prisma version: 1.24.0
I’m not sure what you mean by connector, but I’m using Prisma CLI and hosting on the free database on Prisma Cloud.


#8

Yeah, that is enough information. I wanted to know which underlying database you were using. Prisma Cloud is on MySQL, so that helps me.


#9

Hey, this datamodel looks incomplete, for example, it is missing the type Item. Here is the list of errors that I got when trying to deploy it.

  Phase
    ✖ The field `items` has the type `[Item]` but there's no type or enum declaration with that name.
    ✖ The field `locationType` has the type `_LocationType!` but there's no type or enum declaration with that name.

  Interaction
    ✖ The field `type` has the type `_InteractionType!` but there's no type or enum declaration with that name.

  Answer
    ✖ The field `type` has the type `_AnswerType!` but there's no type or enum declaration with that name.
    ✖ The field `relation` has the type `_Relation` but there's no type or enum declaration with that name.
    ✖ The field `answerTag` has the type `_Trivia` but there's no type or enum declaration with that name.

#10

Hey Divyendu,
I didn’t upload the entire datamodel because I didn’t think I needed to. Should I?


#11

Yes, please, if possible, you can also raise an issue here: https://github.com/prisma/prisma/issues/new?template=bug_report.md

With a minimal reproduction repository. Thanks.


#12

Hey, I can’t post the entire data model but here’s a minimal version that should have the same issue.

type User{
    data: Data! @relation(name: "UserData", onDelete: CASCADE)
}

type Data{
    user: User! @relation(name: "UserData", onDelete: SET_NULL)
    extra: ExtraData! @relation(name: "ExtraData", onDelete: CASCADE)
}

type ExtraData{
    data: Data! @relation(name: "ExtraData", onDelete: SET_NULL)
    info: String!
}

Create a User node and then trying delete said User would cause an error because of the relation “ExtraData”.

I hope this would be sufficient.


#13

Just tried again and it worked. I didn’t change any code but I can now delete the node. Not sure why this is working now without any errors but I guess I’m not complaining. Thank you both for your help!


#14

Thanks, feel free to reach out if this happens again. Happy to help :slight_smile: