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

Cascade deletes doesn't work on many to many relations #2328

Closed
JunquerGJ opened this issue Apr 29, 2020 · 31 comments
Closed

Cascade deletes doesn't work on many to many relations #2328

JunquerGJ opened this issue Apr 29, 2020 · 31 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: cascade topic: referential actions
Milestone

Comments

@JunquerGJ
Copy link

JunquerGJ commented Apr 29, 2020

Bug description

I'm getting an error when I try to remove an entity from a explicit many-to-many relationship.
The change you are trying to make would violate the required relation 'ContactToServerToServer' between the ContactToServerandServer models.

How to reproduce

I am using an empty posgresql database and Typescript so:

  1. npx prisma generate
  2. npx prisma migrate save --name init --experimental
  3. npx prisma migrate up --experimental
  4. DEBUG=* npx ts-node index.ts

Expected behavior

I wanted to delete the server, ( and doing so, delete also the entry in the relation table )
The created contact should not be affected

Prisma information

My prisma schema:

`// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}




model Contact {
  id Int @default(autoincrement()) @id
  name String @unique
  servers ContactToServer[]

}
model ContactToServer {
  contact Contact @relation(fields: [contactId], references: [id])
  contactId Int
  server Server @relation(fields: [serverId], references: [id])
  serverId Int
  functionality String 

  @@id([contactId,serverId])
}
model Server {
  id Int @default(autoincrement()) @id
  hostname String @unique
  contacts ContactToServer[]
}`

and my Prisma queries:

import { PrismaClient } from "@prisma/client"

const prisma = new PrismaClient({
    log : ["info","query","warn"]
})
async function main() {
   await test()
}
async function test(){
    const contact = await prisma.contact.create({
        data : {
            name : "Alice"
        }
    })
    const contact2 = await prisma.contact.create({
        data : {
            name : "Bob"
        }
    })
    const server = await prisma.server.create({
        data : {
            hostname : "SERVER_01",
            contacts : {
                create : [
                    {
                        functionality : "Support",
                        contact : {
                            connect : {
                                name : "Bob"
                            }
                        }
                    },
                    {
                        functionality : "Hosting",
                        contact : {
                            connect : {
                                name : "Alice"
                            }
                        }
                    }
                ]
            }
        }
    })
    const deleteServer = await prisma.server.delete({
        where : {
            id : 1
        }
    })
    return
}
main()
    .catch(e => {
        throw e
    })

Environment & setup

  • OS: Ubuntu 18.04.4 LTS
  • Database: PostgreSQL 11.7
  • Prisma version: prisma/1.34.10 (linux-x64)
  • Node.js version: v12.14.1
  • NPM version: 6.14.4

Stack Trace

prisma-client {
  prisma-client   engineConfig: {
  prisma-client     cwd: '/home/docker/Prisma/prisma',
  prisma-client     debug: false,
  prisma-client     datamodelPath: '/home/docker/Prisma/node_modules/@prisma/client/schema.prisma',
  prisma-client     prismaPath: undefined,
  prisma-client     datasources: [],
  prisma-client     generator: {
  prisma-client       name: 'client',
  prisma-client       provider: 'prisma-client-js',
  prisma-client       output: '/home/docker/Prisma/node_modules/@prisma/client',
  prisma-client       binaryTargets: [],
  prisma-client       config: {}
  prisma-client     },
  prisma-client     showColors: false,
  prisma-client     logLevel: 'info',
  prisma-client     logQueries: true,
  prisma-client     env: {
  prisma-client       DATABASE_URL: 'postgresql://test:test@localhost:5432/test2?schema=public'
  prisma-client     },
  prisma-client     flags: []
  prisma-client   }
  prisma-client } +0ms
  prisma-client Prisma Client call: +67ms
  prisma-client prisma.contact.create({
  prisma-client   data: {
  prisma-client     name: 'Alice'
  prisma-client   }
  prisma-client }) +1ms
  prisma-client Generated request: +0ms
  prisma-client mutation {
  prisma-client   createOneContact(data: {
  prisma-client     name: "Alice"
  prisma-client   }) {
  prisma-client     id
  prisma-client     name
  prisma-client   }
  prisma-client }
  prisma-client  +0ms
  engine {
  engine   PRISMA_DML_PATH: '/home/docker/Prisma/node_modules/@prisma/client/schema.prisma',
  engine   PORT: '39629',
  engine   RUST_BACKTRACE: '1',
  engine   RUST_LOG: 'info',
  engine   LOG_QUERIES: 'true',
  engine   OVERWRITE_DATASOURCES: '[]'
  engine } +0ms
  engine { cwd: '/home/docker/Prisma/prisma' } +0ms
  getos { version: 'OpenSSL 1.1.1  11 Sep 2018\n' } +0ms
  getos { ls: '' } +0ms
  getos { platform: 'linux', libssl: '1.1.x' } +2ms
  plusX Execution permissions of /home/docker/Prisma/node_modules/@prisma/client/runtime/query-engine-debian-openssl-1.1.x are fine +0ms
  engine { flags: [ '--enable-raw-queries' ] } +4ms
  engine stderr Printing to stderr for debugging +15ms
  engine stderr Listening on 127.0.0.1:39629 +1ms
  engine stdout {
  timestamp: 'Apr 29 21:46:40.768',
  level: 'INFO',
  target: 'quaint::pooled',
  fields: { message: 'Starting a postgresql pool with 15 connections.' }
} +0ms
prisma:info  Starting a postgresql pool with 15 connections.
  engine stdout {
  timestamp: 'Apr 29 21:46:40.771',
  level: 'INFO',
  target: 'query_engine::server',
  fields: { message: 'Started http server on 127.0.0.1:39629' }
} +3ms
prisma:info  Started http server on 127.0.0.1:39629
  engine stdout {
  timestamp: 'Apr 29 21:46:40.789',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: { query: 'BEGIN', item_type: 'query', params: '[]', duration_ms: 0 }
} +18ms
prisma:query BEGIN
  engine stdout {
  timestamp: 'Apr 29 21:46:40.790',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'INSERT INTO "public"."Contact" ("name") VALUES ($1) RETURNING "public"."Contact"."id"',
    item_type: 'query',
    params: '["Alice"]',
    duration_ms: 0
  }
} +1ms
prisma:query INSERT INTO "public"."Contact" ("name") VALUES ($1) RETURNING "public"."Contact"."id"
  engine stdout {
  timestamp: 'Apr 29 21:46:40.791',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'SELECT "public"."Contact"."id", "public"."Contact"."name" FROM "public"."Contact" WHERE "public"."Contact"."id" = $1 LIMIT $2 OFFSET $3',
    item_type: 'query',
    params: '[1,1,0]',
    duration_ms: 0
  }
} +1ms
prisma:query SELECT "public"."Contact"."id", "public"."Contact"."name" FROM "public"."Contact" WHERE "public"."Contact"."id" = $1 LIMIT $2 OFFSET $3
  engine stdout {
  timestamp: 'Apr 29 21:46:40.802',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'COMMIT',
    item_type: 'query',
    params: '[]',
    duration_ms: 10
  }
} +11ms
prisma:query COMMIT
  prisma-client Prisma Client call: +69ms
  prisma-client prisma.contact.create({
  prisma-client   data: {
  prisma-client     name: 'Bob'
  prisma-client   }
  prisma-client }) +0ms
  prisma-client Generated request: +0ms
  prisma-client mutation {
  prisma-client   createOneContact(data: {
  prisma-client     name: "Bob"
  prisma-client   }) {
  prisma-client     id
  prisma-client     name
  prisma-client   }
  prisma-client }
  prisma-client  +0ms
  engine stdout {
  timestamp: 'Apr 29 21:46:40.817',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: { query: 'BEGIN', item_type: 'query', params: '[]', duration_ms: 0 }
} +16ms
prisma:query BEGIN
  engine stdout {
  timestamp: 'Apr 29 21:46:40.818',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'INSERT INTO "public"."Contact" ("name") VALUES ($1) RETURNING "public"."Contact"."id"',
    item_type: 'query',
    params: '["Bob"]',
    duration_ms: 0
  }
} +1ms
prisma:query INSERT INTO "public"."Contact" ("name") VALUES ($1) RETURNING "public"."Contact"."id"
  engine stdout {
  timestamp: 'Apr 29 21:46:40.819',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'SELECT "public"."Contact"."id", "public"."Contact"."name" FROM "public"."Contact" WHERE "public"."Contact"."id" = $1 LIMIT $2 OFFSET $3',
    item_type: 'query',
    params: '[2,1,0]',
    duration_ms: 0
  }
} +1ms
prisma:query SELECT "public"."Contact"."id", "public"."Contact"."name" FROM "public"."Contact" WHERE "public"."Contact"."id" = $1 LIMIT $2 OFFSET $3
  engine stdout {
  timestamp: 'Apr 29 21:46:40.821',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: { query: 'COMMIT', item_type: 'query', params: '[]', duration_ms: 1 }
} +1ms
prisma:query COMMIT
  prisma-client Prisma Client call: +10ms
  prisma-client prisma.server.create({
  prisma-client   data: {
  prisma-client     hostname: 'SERVER_01',
  prisma-client     contacts: {
  prisma-client       create: [
  prisma-client         {
  prisma-client           functionality: 'Support',
  prisma-client           contact: {
  prisma-client             connect: {
  prisma-client               name: 'Bob'
  prisma-client             }
  prisma-client           }
  prisma-client         },
  prisma-client         {
  prisma-client           functionality: 'Hosting',
  prisma-client           contact: {
  prisma-client             connect: {
  prisma-client               name: 'Alice'
  prisma-client             }
  prisma-client           }
  prisma-client         }
  prisma-client       ]
  prisma-client     }
  prisma-client   }
  prisma-client }) +0ms
  prisma-client Generated request: +0ms
  prisma-client mutation {
  prisma-client   createOneServer(data: {
  prisma-client     hostname: "SERVER_01"
  prisma-client     contacts: {
  prisma-client       create: [
  prisma-client         {
  prisma-client           functionality: "Support"
  prisma-client           contact: {
  prisma-client             connect: {
  prisma-client               name: "Bob"
  prisma-client             }
  prisma-client           }
  prisma-client         },
  prisma-client         {
  prisma-client           functionality: "Hosting"
  prisma-client           contact: {
  prisma-client             connect: {
  prisma-client               name: "Alice"
  prisma-client             }
  prisma-client           }
  prisma-client         }
  prisma-client       ]
  prisma-client     }
  prisma-client   }) {
  prisma-client     id
  prisma-client     hostname
  prisma-client   }
  prisma-client }
  prisma-client  +0ms
  engine stdout {
  timestamp: 'Apr 29 21:46:40.826',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: { query: 'BEGIN', item_type: 'query', params: '[]', duration_ms: 0 }
} +6ms
prisma:query BEGIN
  engine stdout {
  timestamp: 'Apr 29 21:46:40.827',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'INSERT INTO "public"."Server" ("hostname") VALUES ($1) RETURNING "public"."Server"."id"',
    item_type: 'query',
    params: '["SERVER_01"]',
    duration_ms: 0
  }
} +1ms
prisma:query INSERT INTO "public"."Server" ("hostname") VALUES ($1) RETURNING "public"."Server"."id"
  engine stdout {
  timestamp: 'Apr 29 21:46:40.828',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'SELECT "public"."Contact"."id" FROM "public"."Contact" WHERE "public"."Contact"."name" = $1 OFFSET $2',
    item_type: 'query',
    params: '["Bob",0]',
    duration_ms: 0
  }
} +0ms
prisma:query SELECT "public"."Contact"."id" FROM "public"."Contact" WHERE "public"."Contact"."name" = $1 OFFSET $2
  engine stdout {
  timestamp: 'Apr 29 21:46:40.829',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'INSERT INTO "public"."ContactToServer" ("contactId","serverId","functionality") VALUES ($1,$2,$3) RETURNING "public"."ContactToServer"."contactId", "public"."ContactToServer"."serverId"',
    item_type: 'query',
    params: '[2,1,"Support"]',
    duration_ms: 1
  }
} +2ms
prisma:query INSERT INTO "public"."ContactToServer" ("contactId","serverId","functionality") VALUES ($1,$2,$3) RETURNING "public"."ContactToServer"."contactId", "public"."ContactToServer"."serverId"
  engine stdout {
  timestamp: 'Apr 29 21:46:40.830',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'SELECT "public"."Contact"."id" FROM "public"."Contact" WHERE "public"."Contact"."name" = $1 OFFSET $2',
    item_type: 'query',
    params: '["Alice",0]',
    duration_ms: 0
  }
} +0ms
prisma:query SELECT "public"."Contact"."id" FROM "public"."Contact" WHERE "public"."Contact"."name" = $1 OFFSET $2
  engine stdout {
  timestamp: 'Apr 29 21:46:40.831',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'INSERT INTO "public"."ContactToServer" ("contactId","serverId","functionality") VALUES ($1,$2,$3) RETURNING "public"."ContactToServer"."contactId", "public"."ContactToServer"."serverId"',
    item_type: 'query',
    params: '[1,1,"Hosting"]',
    duration_ms: 0
  }
} +3ms
prisma:query INSERT INTO "public"."ContactToServer" ("contactId","serverId","functionality") VALUES ($1,$2,$3) RETURNING "public"."ContactToServer"."contactId", "public"."ContactToServer"."serverId"
  engine stdout {
  timestamp: 'Apr 29 21:46:40.835',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'SELECT "public"."Server"."id", "public"."Server"."hostname" FROM "public"."Server" WHERE "public"."Server"."id" = $1 LIMIT $2 OFFSET $3',
    item_type: 'query',
    params: '[1,1,0]',
    duration_ms: 3
  }
} +2ms
prisma:query SELECT "public"."Server"."id", "public"."Server"."hostname" FROM "public"."Server" WHERE "public"."Server"."id" = $1 LIMIT $2 OFFSET $3
  engine stdout {
  timestamp: 'Apr 29 21:46:40.836',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: { query: 'COMMIT', item_type: 'query', params: '[]', duration_ms: 1 }
} +2ms
prisma:query COMMIT
  prisma-client Prisma Client call: +14ms
  prisma-client prisma.server.delete({
  prisma-client   where: {
  prisma-client     id: 1
  prisma-client   }
  prisma-client }) +0ms
  prisma-client Generated request: +0ms
  prisma-client mutation {
  prisma-client   deleteOneServer(where: {
  prisma-client     id: 1
  prisma-client   }) {
  prisma-client     id
  prisma-client     hostname
  prisma-client   }
  prisma-client }
  prisma-client  +0ms
  engine stdout {
  timestamp: 'Apr 29 21:46:40.839',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: { query: 'BEGIN', item_type: 'query', params: '[]', duration_ms: 0 }
} +3ms
prisma:query BEGIN
  engine stdout {
  timestamp: 'Apr 29 21:46:40.840',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'SELECT "public"."Server"."id", "public"."Server"."hostname" FROM "public"."Server" WHERE "public"."Server"."id" = $1 LIMIT $2 OFFSET $3',
    item_type: 'query',
    params: '[1,1,0]',
    duration_ms: 0
  }
} +0ms
prisma:query SELECT "public"."Server"."id", "public"."Server"."hostname" FROM "public"."Server" WHERE "public"."Server"."id" = $1 LIMIT $2 OFFSET $3
  engine stdout {
  timestamp: 'Apr 29 21:46:40.841',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'SELECT "public"."ContactToServer"."contactId", "public"."ContactToServer"."serverId", "public"."ContactToServer"."serverId" FROM "public"."ContactToServer" WHERE (1=1 AND "public"."ContactToServer"."serverId" IN ($1)) OFFSET $2',
    item_type: 'query',
    params: '[1,0]',
    duration_ms: 0
  }
} +1ms
prisma:query SELECT "public"."ContactToServer"."contactId", "public"."ContactToServer"."serverId", "public"."ContactToServer"."serverId" FROM "public"."ContactToServer" WHERE (1=1 AND "public"."ContactToServer"."serverId" IN ($1)) OFFSET $2
  engine stdout {
  timestamp: 'Apr 29 21:46:40.841',
  level: 'INFO',
  target: 'quaint::connector::metrics',
  fields: {
    query: 'ROLLBACK',
    item_type: 'query',
    params: '[]',
    duration_ms: 0
  }
} +0ms
prisma:query ROLLBACK
  engine {
  engine   error: PrismaClientKnownRequestError: The change you are trying to make would violate the required relation 'ContactToServerToServer' between the `ContactToServer` and `Server` models.
  engine       at NodeEngine.graphQLToJSError (/home/docker/Prisma/node_modules/@prisma/engine-core/dist/NodeEngine.js:560:1)
  engine       at /home/docker/Prisma/node_modules/@prisma/engine-core/dist/NodeEngine.js:468:1
  engine       at process.result (internal/process/task_queues.js:94:5)
  engine       at PrismaClientFetcher.request (/home/docker/Prisma/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:621:13) {
  engine     code: 'P2014',
  engine     meta: {
  engine       relation_name: 'ContactToServerToServer',
  engine       model_a_name: 'ContactToServer',
  engine       model_b_name: 'Server'
  engine     }
  engine   }
  engine } +1ms
  printStack callsite Error: 
    at Object.model [as Server] (/home/docker/Prisma/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:377:28)
    at Object.n.<computed> [as delete] (/home/docker/Prisma/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:512:33)
    at /home/docker/Prisma/index.ts:49:52
    at step (/home/docker/Prisma/index.ts:33:23)
    at Object.next (/home/docker/Prisma/index.ts:14:53)
    at fulfilled (/home/docker/Prisma/index.ts:5:58)
    at processTicksAndRejections (internal/process/task_queues.js:94:5) +0ms
(node:26476) UnhandledPromiseRejectionWarning: Error: 
Invalid `prisma.server.delete()` invocation in
/home/docker/Prisma/index.ts:49:52

The change you are trying to make would violate the required relation 'ContactToServerToServer' between the `ContactToServer` and `Server` models.
    at PrismaClientFetcher.request (/home/docker/Prisma/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:640:15)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
(node:26476) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:26476) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. process/candidate labels May 4, 2020
@pantharshit00
Copy link
Contributor

I can confirm this bug is present in the latest alpha 2.0.0-alpha.1182

@janpio janpio added tech/engines Issue for tech Engines. and removed tech/engines Issue for tech Engines. labels May 12, 2020
@janpio janpio added this to the Beta 6 milestone May 12, 2020
@janpio janpio modified the milestones: Beta 6, Beta 7, Beta 8 May 26, 2020
@hi019
Copy link

hi019 commented May 31, 2020

Pretty nasty bug; also occurs in the explicit M2M example on the docs

@Sytten
Copy link
Contributor

Sytten commented May 31, 2020

This issue should be renamed to: Support cascade delete in Many-to-Many relationships and be tagged with topic:cascade.

@pantharshit00 pantharshit00 changed the title Error when a entity is deleted in a explicit many-to-many relation Support cascade delete in Many-to-Many relationships Jun 1, 2020
@pantharshit00 pantharshit00 changed the title Support cascade delete in Many-to-Many relationships Cascade deletes doesn't work on many to many relations Jun 2, 2020
@aridgupta
Copy link

Any update on this? This is a very high priority bug which can make or break almost any project.

@CGerAJ
Copy link

CGerAJ commented Aug 10, 2020

@prisma/cli          : 2.4.1
Current platform     : darwin
Query Engine         : query-engine 195d4bdc2d16132977f4ba7a8ca312f7906cb086 (at ../../.nvm/versions/node/v14.0.0/lib/node_modules/@prisma/cli/query-engine-darwin)
Migration Engine     : migration-engine-cli 195d4bdc2d16132977f4ba7a8ca312f7906cb086 (at ../../.nvm/versions/node/v14.0.0/lib/node_modules/@prisma/cli/migration-engine-darwin)
Introspection Engine : introspection-core 195d4bdc2d16132977f4ba7a8ca312f7906cb086 (at ../../.nvm/versions/node/v14.0.0/lib/node_modules/@prisma/cli/introspection-engine-darwin)
Format Binary        : prisma-fmt 195d4bdc2d16132977f4ba7a8ca312f7906cb086 (at ../../.nvm/versions/node/v14.0.0/lib/node_modules/@prisma/cli/prisma-fmt-darwin)

Now I can only look at database relationships and delete them using $queryraw.

@matthewmueller matthewmueller removed this from the Beta 10 milestone Aug 24, 2020
@blms
Copy link

blms commented Aug 24, 2020

This issue seems to also affect disconnect and set fields in CRUD mutations.


Querying my data to show initial state

Query where $id = doc1:

query getDoc ($id: String) {
  document(where: {id:$id}){
    documentKind{
      id
      KindId
      DocumentId
    }
  }
}

Result:

{
  "data": {
    "document": {
      "documentKind": [
        {
          "id": "cuid1",
          "KindId": "kind1",
          "DocumentId": "doc1"
        },
        {
          "id": "cuid2",
          "KindId": "kind2",
          "DocumentId": "doc1"
        }
      ]
    }
  }
}

Now I want to disconnect the documentKind with id = cuid2.

Mutation
mutation updateDoc($id:String, $data:DocumentUpdateInput!) {
  updateOneDocument(where:{id:$id},data:$data){
    documentKind {
      KindId
      DocumentId
    }
  }
}
Attempt 1 with "disconnect"

Variables:

{
  "id": "doc1",
  "data": {
    "documentKind": {
      "disconnect": {
        "KindOnDocument_AB_unique": {
          "KindId": "kind2",
          "DocumentId": "doc1"
        }
      }
    }
  }
}

Result:

{
  "error": {
    "errors": [
      {
        "message": "Invalid `prisma.document.update()` invocation in /node_modules/nexus-plugin-prisma/dist/schema/builder.js:202:93

        198         locallyComputedInputs: publisherConfig.locallyComputedInputs,
        199     });
        200 }
        201 args = this.paginationStrategy.resolve(args);
        → 202 return photon[mappedField.photonAccessor][mappedField.operation](args)

        The change you are trying to make would violate the required relation 'DocumentToKindOnDocument' between the `Document` and `KindOnDocument` models.",

        "locations": [
          {
            "line": 2,
            "column": 3
          }
        ],
        "path": [
          "updateOneDocument"
        ]
      }
    ],
    "data": {
      "updateOneDocument": null
    }
  }
}
Attempt 2 with "set"

Variables:

{
  "id": "doc1",
  "data": {
    "documentKind": {
      "set": [{
        "KindOnDocument_AB_unique": {
          "KindId": "kind1",
          "DocumentId": "doc1"
        }
      }]
    }
  }
}

Result: Same as above.


Note that I'm using nexus-plugin-prisma. Let me know if I should open a new issue (here or there).

@ghost
Copy link

ghost commented Oct 10, 2020

I am also having this issue. More than 5 months since this has been opened an there is still no fix? What's the deal?

@michaeleliot
Copy link

Could this be related to #2057?

@allexxis
Copy link

allexxis commented Nov 18, 2020

I'm still getting this error until this day using when foreign key constraint is set with on DELETE CASCADE with one to one relationship on a PostgreSQL table using Version 12

@Sayeck
Copy link

Sayeck commented Dec 17, 2020

If you want to delete nested records in cascade deleting the parent, before you need set constraint key as nullable in the database or in your schema. This solve the issue.

@Sytten
Copy link
Contributor

Sytten commented Dec 17, 2020

This is actually a bug as it should not be needed for cascade, see #4650

@Hugoo
Copy link

Hugoo commented Jan 27, 2021

I'm also facing this issue.

When deleting directly in the db (or by using tools like pgAdmin), N:M relation is correctly deleted, the prisma client prevents performing this delete operation.

@sayertindall
Copy link

sayertindall commented Feb 25, 2021

What is the status with this? What is the solution? Has been almost a year since this was opened

@Ebdulmomen1
Copy link

so what is the solution when you face this? this is not a minor bug! what can i do to go forward with my project? if i reinitalize prisma from the start, will i face this issue again?

@JunquerGJ
Copy link
Author

JunquerGJ commented Feb 27, 2021

so what is the solution when you face this? this is not a minor bug! what can i do to go forward with my project? if i reinitalize prisma from the start, will i face this issue again?

You will have this error again until it is fixed.

Straight to the point, I have an API endpoint which will call to prisma.ENTITY.deleteMany. If I do just that, the bug we are talking about will be thrown. To "fix" this, I added a call to prisma.executeRaw and deleting the things I want deleted in the database "manually" Documentation here

@yassinebridi
Copy link

@JunquerGJ Thanks, i think this is what i'm going to do for now, otherwise the project i'm working on is frozen because of this bug. I hope they prioritize this issue.

@janpio janpio added the team/schema Issue for team Schema. label Mar 1, 2021
@zub0r
Copy link

zub0r commented Mar 18, 2021

Cascade delete currently doesn't work for me for 1:n relations either.
The change you are trying to make would violate the required relation 'AToB' between the 'A' and 'B' models.
prisma (2.19.0)

@philippfo9
Copy link

Can confirm that I'm having the same issue with a 1:n relationship (latest prisma: 2.20.1)

@euberdeveloper
Copy link

This issue is opened by almost one year. How is it possible that with all the huge work that the prisma team is doing to improve the library, no solution has been provided yet? This is quite a big issue, and I think that it should also be labeled as bug.

@philippfo9
Copy link

@euberdeveloper maybe, this is just a small issue seen relatively and not prioritized so highly

@euberdeveloper
Copy link

@philippfo9 Yes, I guess, but it does not seem a tricky fix, it is the default behaviour of SQL cascade to delete the related tuples (I guess the problem is because of prisma checks).

@Hugoo
Copy link

Hugoo commented Apr 14, 2021

@philippfo9 Yes, I guess, but it does not seem a tricky fix, it is the default behaviour of SQL cascade to delete the related tuples (I guess the problem is because of prisma checks).

It's time for one of us to send a PR 😁 🤙

@onceLearner
Copy link

onceLearner commented Apr 27, 2021

This is an annoying bug ! I am somehow disappointed + been so long since it is open

@Dammmien
Copy link

Dammmien commented May 7, 2021

I'm having this issue too, it's very annoying because this is natively supported by PostgreSQL and it's one of the advantage of using a relational database. More over this behavior is completely valid and a raw query works as expected, so the prisma client is preventing a legit behavior. Can we open a PR on our own ? Is it a tricky change, can we have some guidelines to work on it ?

@pimeys
Copy link
Contributor

pimeys commented May 7, 2021

We are actively working on this. This week the schema working group made their decision on how it should look like in the data model. The next and final step is the implementation.

@unix
Copy link

unix commented May 22, 2021

Is there any way we can get around here until the new fix is released?
If I execute SQL (prisma.$executeRaw) myself to remove some M2M data, do I also need to manually remove the relational tables implicitly generated by Prisma, I'm not sure if manually removing data from relational tables will affect the operation of Prisma or future queries.

If the official can give a best practice I think it will definitely help a lot. (a solution for everyone until the problem is fixed)

@erencay
Copy link

erencay commented Jun 8, 2021

Our project heavily depends on cascade deletions. After weeks of work to migrate from Typeorm to Prisma, we faced with this issue. This is big, Prisma should've mention this shortcoming in limitations so we would wait until it fixed. This not some behavior you would expect from a "production-ready" software!

@pimeys
Copy link
Contributor

pimeys commented Jun 22, 2021

Fixed in prisma/prisma-engines#1947
Proposal for you all to read in #6996

Instructions on how to use this, and a place to give feedback: #7816

@pimeys pimeys closed this as completed Jun 22, 2021
@Jolg42 Jolg42 added this to the 2.26.0 milestone Jun 28, 2021
@janpio
Copy link
Member

janpio commented Jun 29, 2021

This has now been released as a preview feature flag. You can read about it in the release notes for 2.26.0: https://github.com/prisma/prisma/releases/tag/2.26.0 If you have any feedback, please use this issue: #7816

@MoathDawahreh
Copy link

MoathDawahreh commented Apr 18, 2024

It still does not work with implicit many-many relations

@janpio
Copy link
Member

janpio commented Apr 19, 2024

Then please open a new issue and provide a full reproduction. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: cascade topic: referential actions
Projects
None yet
Development

No branches or pull requests