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 add foreign key constraint for MySQL migration that tries to create foreign key pointing to non-PK and unindexed field #13339

Open
janpio opened this issue May 12, 2022 · 1 comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/schema Issue for team Schema. topic: mysql topic: prisma migrate dev CLI: prisma migrate dev

Comments

@janpio
Copy link
Member

janpio commented May 12, 2022

This schema:

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

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

model A {
  id         Int          @id @default(autoincrement())
  custom_id  String
  B          B[]
}

model B {
  id        Int     @id @default(autoincrement())
  a_id String
  A         A @relation(fields: [a_id], references: [custom_id])
}

(Note how custom_id is not indexed at all [and also not unique, which might be invalid, see issue https://github.com//issues/13340)

leads to this migration SQL:

-- CreateTable
CREATE TABLE `A` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `custom_id` VARCHAR(191) NOT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `B` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `a_id` VARCHAR(191) NOT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `B` ADD CONSTRAINT `B_a_id_fkey` FOREIGN KEY (`a_id`) REFERENCES `A`(`custom_id`) ON DELETE RESTRICT ON UPDATE CASCADE;

Which on execution fails with:

Error: P3018

A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve

Migration name: 20220512183128_init

Database error code: 1215

Database error:
Cannot add foreign key constraint

Please check the query number 3 from the migration file.

Adding an index @@index([custom_id]) to A makes the migration pass.

@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. topic: mysql team/schema Issue for team Schema. topic: prisma migrate dev CLI: prisma migrate dev labels May 12, 2022
@janpio
Copy link
Member Author

janpio commented May 12, 2022

This issue might become obsolete if #13340 is fixed by changing the validation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/schema Issue for team Schema. topic: mysql topic: prisma migrate dev CLI: prisma migrate dev
Projects
None yet
Development

No branches or pull requests

1 participant