1

I'm working with Prisma and I want to support CASCADE delete but although I have done everything mentioned in the docs I'm still not getting it to work

this is the error I'm getting when I try to Prisma deploy

Errors:

  Restaurant
    ✖ The Mongo connector currently does not support Cascading Deletes, but the field `foods` defines cascade behavior. Please remove the onDelete argument.}

Here is the code

type Restaurant {
      id: ID! @id
      name: String!
      foods: [Food!]! @relation(onDelete: CASCADE, name: "FoodToRestaurant", link: INLINE)
    }

    type Food {
      id: ID! @id
      name: String!
      desc: String
      price: Float!
      category: Category!
      restaurant: Restaurant! @relation(name: "FoodToRestaurant", onDelete: SET_NULL)
    }

I expect when the restaurant gets deleted all its foods should also be deleted

I have CASCADE deleted with Prisma PostgreSQL but now I want to use MongoDB for this app

  • 1
    Since the Prisma MongoDB connector does not currently support cascading deletes (see [GitHub issue #3796](https://github.com/prisma/prisma/issues/3796)), the error message is expected. – Stennie May 04 '19 at 15:04

3 Answers3

0

currently mongodb prisma not support cascading delete. if you have it in your schema please remove it.

TheEhsanSarshar
  • 2,677
  • 22
  • 41
0

As it's not possible, you should manage it manually.

Means you should delete all of related entities recursively.

For example if it's your db schema:

Question -> Comment -> Rate

If you want to delete a question, you should delete all of comments related to that question, and if you want to delete a comment, you should delete all of rates assigned to that comment. So you need some recursive functions for deleting these entities.

function deleteQuestion(questionId) {
  for each comment related to questionID
    deleteComment(commentId)
  delete(questionId)       
}

function deleteComment(commentId) {
  for each rate related to commentId
    deleteRate(rateId)
  delete(commentId)       
}

function deleteRate(rateId) {
  delete(rateId)       
}
Emad Armoun
  • 1,625
  • 2
  • 19
  • 32
0

With referential action, you can have cascade delete for this Prisma version 3.7.0 and later is needed. check out the Types of referential actions table https://www.prisma.io/docs/concepts/components/prisma-schema/relations/referential-actions#cascade