prisma / prisma

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
https://www.prisma.io
Apache License 2.0
38.98k stars 1.53k forks source link

Prisma re-generates and drops default on `migrate dev` #8366

Open casey-chow opened 3 years ago

casey-chow commented 3 years ago

Bug description

When running prisma migrate dev, the generated schema drops the default value for a complex default value, and its corresponding sequence.

How to reproduce

  1. Use the below schema.
  2. Generate a schema using prisma migrate dev, modify to look like the SQL migration below.
  3. Generate another schema using prisma migrate dev.

(Note that the default in Postgres is also the outputted value from Postgres when examining generated DDL.)

Expected behavior

The second invocation of migrate dev shouldn't generate another migration.

Prisma information

Schema Used

model User {
    id         Int @id @default(autoincrement())
    externalId Int @default(dbgenerated("pseudo_encrypt((nextval('user_eid_seq'::regclass))::integer)"))
}

SQL Migration

... pseudo-encrypt function from https://wiki.postgresql.org/wiki/Pseudo_encrypt ...
CREATE SEQUENCE user_eid_seq MAXVALUE 2147483647;
CREATE TABLE "user" (
      id SERIAL NOT NULL,
      eid integer DEFAULT pseudo_encrypt((nextval('user_eid_seq'::regclass))::integer) NOT NULL
);

Environment & setup

Prisma Version

prisma               : 2.27.0
@prisma/client       : 2.27.0
Current platform     : darwin
Query Engine         : query-engine cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : cdba6ec525e0213cce26f8e4bb23cf556d1479bb
Studio               : 0.410.0
thebiglabasky commented 3 years ago

We'll need to table this until there's more capacity.

sibeliuz commented 2 years ago

Bug still exists.

adamlong5 commented 2 years ago

I'm seeing this same behavior with a different @default(dbgenerated()) value. Also on Postgres on Prisma 2.23.0. Every new migration I make, it tries to add an ALTER line to set the column's default value.