Skip to content
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

Cannot delete a tuple on which a relation depends #6452

Closed
euberdeveloper opened this issue Apr 7, 2021 · 1 comment
Closed

Cannot delete a tuple on which a relation depends #6452

euberdeveloper opened this issue Apr 7, 2021 · 1 comment

Comments

@euberdeveloper
Copy link

Bug description

I have a model Contratto (contract). Every contract has one or more guests and the relation ContrattoSuOspite (contract-on-guests) is a relation made exactly to connect the guests to a contract. There is the delete cascade, so there should be no problem in deleting the contract. Instead, I have an error.

How to reproduce

The (simplified) schema is:

model Contratto {
    id Int @id @default(autoincrement()) @map("id")

    dataInizio DateTime @map("data_inizio")
    dataFine   DateTime @map("data_fine")
    checkout Float?   @map("checkout")
    cauzione Float?   @map("cauzione")
    note String? @map("note")

    contrattiSuOspite ContrattoSuOspite[]

    @@map("contratto")
}


model Ospite {
    id Int @id @default(autoincrement()) @map("id")
    nome String @map("nome")
}

model ContrattoSuOspite {
    idContratto Int       @map("id_contratto")
    contratto   Contratto @relation(fields: [idContratto], references: [id])

    idOspite Int    @map("id_ospite")
    ospite   Ospite @relation(fields: [idOspite], references: [id])

    dataCheckin          DateTime? @map("data_checkin")
    dataCheckout         DateTime? @map("data_checkout")

    @@id([idContratto, idOspite])
    @@map("contratto_su_ospite")
}

Then I try to delete a contratto:

await prisma.contratto.delete({ where: { id: 1 } });

Expected behavior

It should delete both the contratto and its contratto-su-ospite (contract-on-guest) tuples.

Prisma information

I get this error instead:

[ERROR] Server error PrismaClientKnownRequestError2 [PrismaClientKnownRequestError]: 
Invalid `prisma.contratto.delete()` invocation:


  The change you are trying to make would violate the required relation 'ContrattoToContrattoSuOspite' between the `Contratto` and `ContrattoSuOspite` models.
    at cb (/home/euber/Github/150/operatn-backend/node_modules/@prisma/client/runtime/index.js:79160:17)
    at processTicksAndRejections (node:internal/process/task_queues:94:5) {
  code: 'P2014',
  clientVersion: '2.18.0',
  meta: {
    relation_name: 'ContrattoToContrattoSuOspite',
    model_a_name: 'Contratto',
    model_b_name: 'ContrattoSuOspite'
  }
}

I also do not want to set the relation optional, because it is not and it is not necessary because of the delete cascade.

Environment & setup

  • OS: Ubuntu 20
  • Database: Postgres
  • Node.js version: 15.11.0
  • Prisma version: 2.18

@janpio
Copy link
Member

janpio commented Apr 7, 2021

Duplicate of #2328

@janpio janpio marked this as a duplicate of #2328 Apr 7, 2021
@janpio janpio closed this as completed Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants