Skip to content

Migrate creates unnecessary migrations #5244

@f0rr0

Description

@f0rr0

Bug description

Running prisma migrate keeps dropping and re adding the same foreign key constraints over and over.

How to reproduce

Repro: https://github.com/f0rr0/prisma-migrate-bug-repro

Given the schema

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

datasource db {
  provider = "postgresql"
  url      = "postgresql://prisma:[email protected]:5432/database"
}

model User {
  /// The value of this field is generated by the database as: `gen_random_uuid()`.
  id            String          @id @default(dbgenerated())
  UserStoryLike UserStoryLike[]

  @@map(name: "users")
}

model UserStoryLike {
  storyId String
  userId  String
  Story   Story  @relation(fields: [storyId], references: [id])
  User    User   @relation(fields: [userId], references: [id])

  @@id([storyId, userId])
  @@map(name: "user-story-likes")
}

model Story {
  /// The value of this field is generated by the database as: `gen_random_uuid()`.
  id            String          @id @default(dbgenerated())
  UserStoryLike UserStoryLike[]

  @@map(name: "stories")
}

Running prisma migrate dev --create-only --preview-feature gives the following migration file.

-- CreateTable
CREATE TABLE "users" (
    "id" TEXT NOT NULL,

    PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "user-story-likes" (
    "storyId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,

    PRIMARY KEY ("storyId","userId")
);

-- CreateTable
CREATE TABLE "stories" (
    "id" TEXT NOT NULL,

    PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "user-story-likes" ADD FOREIGN KEY("storyId")REFERENCES "stories"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user-story-likes" ADD FOREIGN KEY("userId")REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

After editing the above to following

CREATE EXTENSION IF NOT EXISTS pgcrypto;

-- CreateTable
CREATE TABLE "users" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),

    PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "user-story-likes" (
    "storyId" UUID NOT NULL,
    "userId" UUID NOT NULL,

    PRIMARY KEY ("storyId","userId")
);

-- CreateTable
CREATE TABLE "stories" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),

    PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "user-story-likes" ADD FOREIGN KEY("storyId")REFERENCES "stories"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user-story-likes" ADD FOREIGN KEY("userId")REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

and applying through prisma migrate dev --preview-feature succeeds but generates another migration:

-- DropForeignKey
ALTER TABLE "user-story-likes" DROP CONSTRAINT "user-story-likes_storyId_fkey";

-- DropForeignKey
ALTER TABLE "user-story-likes" DROP CONSTRAINT "user-story-likes_userId_fkey";

-- AddForeignKey
ALTER TABLE "user-story-likes" ADD FOREIGN KEY("storyId")REFERENCES "stories"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user-story-likes" ADD FOREIGN KEY("userId")REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

All further invocations of migrate include the above migration.

Expected behavior

The unnecessary migration is not created.

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Node.js version: 15.5.1
  • Prisma version:
@prisma/cli          : 2.14.0
@prisma/client       : 2.14.0
Current platform     : darwin
Query Engine         : query-engine 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Studio               : 0.332.0

Metadata

Metadata

Assignees

Labels

bug/1-unconfirmedBug should have enough information for reproduction, but confirmation has not happened yet.kind/bugA reported bug.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions