graphqlprismaprisma-graphqlnexus-prisma

How can I delete items with relations?


I'm using prisma2, and I don't know how to delete items having relations with other models.

This is my models.

model User {
  id       String @default(cuid()) @id
  email    String @unique
  password String
  name     String

  teams       Team[]
  memberships Membership[]
}

model Team {
  id        String   @default(cuid()) @id

  name        String
  founder     User?
  memberships Membership[]
}

model Membership {
  id        String   @default(cuid()) @id

  class    String

  owner User
  team  Team
}

User-Team is 1:n relationship. Team-Membership is 1:n relationship.

And I wanna delete a Team.

I've tried this.

t.list.field("deleteTeam", {
  type: "Team",
  args: {
    teamid: idArg()
  },
  resolve: (_, { teamid }, ctx) => {
    return ctx.photon.teams.deleteMany({
      where: { id: teamid }
    });
  }
});

But it doensn't work because it violates relation.

How can I delete team with disconnecting all the relations at the same time?


Solution

  • Deletes that have dependent relationships usually require that a cascading delete be specified.

    Based on your model, I believe you need to update your graphql schemas to handle either CASCADE on SET_NULL for relations onDelete.

    I know that in other systems like 8base there is a force: Boolean flag that can be specified solve this. However, here is the Prisma docs section for your problem: https://prisma-docs.netlify.com/docs/1.4/reference/prisma-api/concepts-utee3eiquo/#cascading-deletes