Code Monkey home page Code Monkey logo

Comments (4)

janpio avatar janpio commented on July 28, 2024 2

Quick superficial investigation shows that this might be a general SQLite problem in Prisma, not just D1-specific. Requires experimental confirmation.

from prisma.

Jolg42 avatar Jolg42 commented on July 28, 2024 1

I can confirm the error.

The error on the webpage is

PrismaClientKnownRequestError: Invalid `prisma.user.deleteMany()` invocation: Foreign key constraint failed on the field: `foreign key`

{
    "name": "PrismaClientKnownRequestError",
    "code": "P2003",
    "clientVersion": "5.14.0",
    "meta": {
        "modelName": "User",
        "field_name": "foreign key"
    }
}

The error in the console is

✘ [ERROR] Error in performIO: Error: D1_ERROR: FOREIGN KEY constraint failed

      at D1Database._sendOrThrow (cloudflare-internal:d1-api:66:19)
      at async D1PreparedStatement.run (cloudflare-internal:d1-api:178:29)
      ... 2 lines matching cause stack trace ...
      at null.<anonymous> (async
  file:///Users/j42/Repros/prisma-24293/.wrangler/tmp/dev-MvOEWC/index.js:1707:18) {
    [cause]: Error: FOREIGN KEY constraint failed
        at D1Database._sendOrThrow (cloudflare-internal:d1-api:67:24)
        at async D1PreparedStatement.run (cloudflare-internal:d1-api:178:29)
        at async PrismaD1.performIO
  (file:///Users/j42/Repros/prisma-24293/node_modules/@prisma/adapter-d1/dist/index.mjs:230:19)
        at async PrismaD1.executeRaw
  (file:///Users/j42/Repros/prisma-24293/node_modules/@prisma/adapter-d1/dist/index.mjs:222:17)
        at null.<anonymous> (async
  file:///Users/j42/Repros/prisma-24293/.wrangler/tmp/dev-MvOEWC/index.js:1707:18)
  }

It shows there that the error returned by D1 is D1_ERROR: FOREIGN KEY constraint failed which does not contain any information about the FK name.

from prisma.

Jolg42 avatar Jolg42 commented on July 28, 2024 1

Unfortunately, there is nothing we can do about this because SQLite itself does not know the name of the FK.

Example when using sqlite provider, the same happens:
#10867

Some explanation can be found here
https://sqlite.org/forum/info/26c886864e6cfe7bcfd69b1eb52fa57da500c0b7d8ee17d6bad353c39d14a299

The Foreign Key constraint mechanism works by using a counter of failed constraints. When a constraint fails the counter increments. When a constraint is resolved later on within the same statement, the counter decrements. If at the end of the statement (or transaction) the counter is non-zero, then you get an error.

I think the only thing doable here is to change "field_name": "foreign key" to "field_name": "<unknown>", if that's interesting for you, creating a new issue focussed on this improvement would be better suited I think.

from prisma.

Jolg42 avatar Jolg42 commented on July 28, 2024 1

Note that if you wanted to delete all the data in your script, you could first delete the posts and then the users:

await prisma.post.deleteMany();
await prisma.user.deleteMany();

from prisma.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.