I am using PostgreSQL with GraphQL when I try to delete any company that has relation with other tables. it shows GraphQL error:

Viewed 894

tables schema

type Company {
  id: ID! @unique
  name: String!
  users: [CompanyUser!]! @relation(name: "CompanyUserToCompany")
  areas: [Area!]! @relation(name: "Company_areas")
  positions: [Position!]! @relation(name: "Company_positions", onDelete: CASCADE)

}

type Position {
  id: ID! @unique
  name: String!
  users: [CompanyUser!]! @relation(name: "Position_company_user")
  company: Company! @relation(name: "Position_company", onDelete: CASCADE)

}

Shows this GraphQL Error

Error: GraphQL error: The change you are trying to make would violate the required relation 'Position_company' between Company and Position

1 Answers

If you're using prisma2, I guess my answer could help you.

  • Do not delete a item by Company. ( EX : prisma.company.delete({where: ~ }) )

  • Delete a item by Position. ( Ex. prisma.position.delete({where:{company:{id:companyId}}}) )

  • If it doesn't work and okay to change field attribute, make company field nullable( not required ). Then, delete Company first and delete Position( cuz I guess you want cascade, right? )

Don't forget to use await!

I solved this problem in this way. But I'm not sure for case in onDelete:CASCADE. I reaaaally hope prisma2 release cascade soon..!

Related