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

BigInt with a default value cause duplicates in all migrations #14063

Closed
AndersonAO opened this issue Jun 29, 2022 · 1 comment · Fixed by prisma/prisma-engines#3032
Closed
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: broken migrations topic: default
Milestone

Comments

@AndersonAO
Copy link

Bug description

Using this model

model product {
  id        Int      @id @default(autoincrement())
  name      String
  price     BigInt   @default(0)
  discount  BigInt   @default(1)
  stock     BigInt?
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
}

When I run the following command:
npx prisma migrate dev --name init

This generates the following migration:

-- CreateTable
CREATE TABLE "product" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "price" BIGINT NOT NULL DEFAULT 0,
    "discount" BIGINT NOT NULL DEFAULT 1,
    "stock" BIGINT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "product_pkey" PRIMARY KEY ("id")
);

So far everything is going as expected.
But when I run the migration again with the command:
npx prisma migrate dev

Generate this migration:

-- AlterTable
ALTER TABLE "product" ALTER COLUMN "price" SET DEFAULT 0,
ALTER COLUMN "discount" SET DEFAULT 1;

Even though I didn't make any changes to the schema.prisma file.
And this is repeated for all future migrations.

How to reproduce

Init prisma with postgres
Create a model with BigInt fields and add default values
Create a migration
Run migrations

Expected behavior

Migration should only be created once

Prisma information

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

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

model product {
  id        Int      @id @default(autoincrement())
  name      String
  price     BigInt   @default(0)
  discount  BigInt   @default(1)
  stock     BigInt?
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
}

Environment & setup

  • OS: Zorin OS
  • Database: PostgreSQL
  • Node.js version: v14.17.6

Prisma Version

prisma                  : 4.0.0
@prisma/client          : 4.0.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : da41d2bb3406da22087b849f0e911199ba4fbf11
Studio                  : 0.465.0
@AndersonAO AndersonAO added the kind/bug A reported bug. label Jun 29, 2022
@pimeys pimeys added team/schema Issue for team Schema. topic: default topic: broken migrations bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jun 30, 2022
@janpio janpio added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jul 4, 2022
@janpio
Copy link
Member

janpio commented Jul 4, 2022

I can reproduce this, minimal schema:

model foo {
  id  Int    @id @default(autoincrement())
  bar BigInt @default(0)
}

Every migrate dev now generates the following SQL:

-- AlterTable
ALTER TABLE "foo" ALTER COLUMN "bar" SET DEFAULT 0;

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: broken migrations topic: default
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants