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

Bug: Migrations not compatible with D1 #24208

Closed
hrueger opened this issue May 15, 2024 · 2 comments · Fixed by prisma/prisma-engines#4871
Closed

Bug: Migrations not compatible with D1 #24208

hrueger opened this issue May 15, 2024 · 2 comments · Fixed by prisma/prisma-engines#4871
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: d1 Issues related to Cloudflare D1 topic: migrate topic: @prisma/adapter-d1
Milestone

Comments

@hrueger
Copy link

hrueger commented May 15, 2024

Bug description

Generated migrations for D1 (following this guide) are not compatible with D1. When changing a table with relations, foreign keys are disabled and re-enabled with PRAGMA foreign_keys=OFF; and PRAGMA foreign_keys=ON;.
However, D1 seems to need PRAMGA defer_foreign_keys=ON; and PRAGMA defer_foreign_keys=OFF; (also notice the swapped ON and OFF).

When applied, the migration fails with ✘ [ERROR] FOREIGN KEY constraint failed.

How to reproduce

Minimal reproduction: https://github.com/hrueger/prisma-24208

Expected behavior

Migrations are generated with PRAGMA defer_foreign_keys instead so that the migration can be applied.

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())
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?
  posts     Post[]
  // rename this field, let's say to `newField`
  oldField  String?
}

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

Environment & setup

  • OS: macOS
  • Database: Cloudflare D1 / SQLite
  • Node.js version: 21.6.1

Prisma Version

prisma                  : 5.14.0
@prisma/client          : 5.14.0
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v21.6.1
Query Engine (Node-API) : libquery-engine e9771e62de70f79a5e1c604a2d7c8e2a0a874b48 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli e9771e62de70f79a5e1c604a2d7c8e2a0a874b48 (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.14.0-25.e9771e62de70f79a5e1c604a2d7c8e2a0a874b48
Default Engines Hash    : e9771e62de70f79a5e1c604a2d7c8e2a0a874b48
Studio                  : 0.500.0
Preview Features        : driverAdapters
@hrueger hrueger added the kind/bug A reported bug. label May 15, 2024
hrueger added a commit to hrueger/prisma-24208 that referenced this issue May 15, 2024
@janpio
Copy link
Member

janpio commented May 15, 2024

For completeness, can you include the generated migration SQL that is causing problems? Thanks.

@hrueger
Copy link
Author

hrueger commented May 15, 2024

Sure!
generated & not working:

-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_User" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "email" TEXT NOT NULL,
    "name" TEXT,
    "newField" TEXT
);
INSERT INTO "new_User" ("createdAt", "email", "id", "name") SELECT "createdAt", "email", "id", "name" FROM "User";
DROP TABLE "User";
ALTER TABLE "new_User" RENAME TO "User";
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
PRAGMA foreign_key_check("User");
PRAGMA foreign_keys=ON;

expected & working:

-- RedefineTables
PRAGMA defer_foreign_keys=ON;
CREATE TABLE "new_User" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "email" TEXT NOT NULL,
    "name" TEXT,
    "newField" TEXT
);
INSERT INTO "new_User" ("createdAt", "email", "id", "name") SELECT "createdAt", "email", "id", "name" FROM "User";
DROP TABLE "User";
ALTER TABLE "new_User" RENAME TO "User";
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
PRAGMA foreign_key_check("User");
PRAGMA defer_foreign_keys=OFF;

@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. topic: @prisma/adapter-d1 topic: d1 Issues related to Cloudflare D1 labels May 15, 2024
@Weakky Weakky added bug/2-confirmed Bug has been reproduced and confirmed. topic: migrate team/schema Issue for team Schema. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. labels May 16, 2024
jkomyno added a commit to prisma/prisma-engines that referenced this issue May 16, 2024
@jkomyno jkomyno self-assigned this May 16, 2024
@jkomyno jkomyno added this to the 5.15.0 milestone May 16, 2024
jkomyno added a commit to prisma/prisma-engines that referenced this issue May 28, 2024
* fix(schema-engine): make "migrate diff" deterministic

* ok

* fix(schema-engine): combine foreign_keys + defer_foreign_keys PRAGMAs to fix prisma/prisma#24208
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: d1 Issues related to Cloudflare D1 topic: migrate topic: @prisma/adapter-d1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants