Skip to content

Bug (D1 migrations): IDs of optional relations are lost quietly during migration #24540

@hrueger

Description

@hrueger

Bug description

(continues from #24539)

When I have an optional relation defined (using Cloudflare D1) with the default onDelete: SetNull and generate a migration to update a table, the relation ids are lost due to the migration deleting those rows.

How to reproduce

Please follow the instructions in the readme here: https://github.com/hrueger/prisma-24540

Expected behavior

Relation ids are kept or (if not possible), a big fat warning is printed when generating the migration (and also written to the .sql file).
Luckily, I caught that error in our staging environment, but it could have easily happend in production 😬

Prisma information

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["driverAdapters"]
}

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

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  posts     Post[]
  // name      String @default("Anonymous") // uncomment this line
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  author    User?    @relation(fields: [authorId], references: [id])
  authorId  Int?
}

Environment & setup

  • OS: MacOS
  • Database: Cloudflare D1
  • Node.js version: v21.6.1

Prisma Version

prisma                  : 5.15.0
@prisma/client          : 5.15.0
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v21.6.1
Query Engine (Node-API) : libquery-engine 12e25d8d06f6ea5a0252864dd9a03b1bb51f3022 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli 12e25d8d06f6ea5a0252864dd9a03b1bb51f3022 (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.15.0-29.12e25d8d06f6ea5a0252864dd9a03b1bb51f3022
Default Engines Hash    : 12e25d8d06f6ea5a0252864dd9a03b1bb51f3022
Studio                  : 0.501.0
Preview Features        : driverAdapters

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions