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
37.68k stars 1.46k forks source link

Migration is created for the `@default(dbgenerated(...))` value on `DateTime` although already migrated #24240

Open MaximTovstashev opened 2 weeks ago

MaximTovstashev commented 2 weeks ago

Bug description

When new table is created with @default(dbgenerated("NOW() + interval '1 day'")) @db.Timestamp(6) column and the migration is applied, next run of npx prisma migrate dev requests to create a new migration for the same column.

How to reproduce

  1. For the postgres database create a new table with field like
    expire_at  DateTime  @default(dbgenerated("NOW() + interval '1 day'")) @db.Timestamp(6)
  2. Create an apply a migration. Migration file will contain the correct definition for the column:
    "expire_at" TIMESTAMP(6) NOT NULL DEFAULT NOW() + interval '1 day',
  3. Run npx prisma migrate dev without changing anything else
  4. You will be prompted to create a new migration. Give it a name and confirm
  5. New migration will be created, containing a single clause
    ALTER TABLE "employee_accounts_transactions" ALTER COLUMN "expire_at" SET DEFAULT NOW() + interval '1 day';
  6. Repeat steps 3 and 4 — new migration with the same content of step 5 will be created

Expected behavior

No new migrations are created after the initial one

Prisma information

model employee_accounts_transactions {
    id                                  Int                                     @id @default(autoincrement())
    amount                              Decimal                                 @db.Decimal(10, 2)
    reason                              Json
    initiator                           Json

    created_at                          DateTime                                @default(now()) @db.Timestamp(6)
    updated_at                          DateTime?                               @updatedAt @db.Timestamp(6)
    expire_at                           DateTime                                @default(dbgenerated("NOW() + interval '1 day'")) @db.Timestamp(6)
}
// Add your code using Prisma Client

Environment & setup

Prisma Version

prisma                  : 5.14.0
@prisma/client          : 5.14.0
Computed binaryTarget   : debian-openssl-1.0.x
Operating System        : linux
Architecture            : x64
Node.js                 : v20.10.0
Query Engine (Node-API) : libquery-engine e9771e62de70f79a5e1c604a2d7c8e2a0a874b48 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.0.x.so.node)
Schema Engine           : schema-engine-cli e9771e62de70f79a5e1c604a2d7c8e2a0a874b48 (at node_modules/@prisma/engines/schema-engine-debian-openssl-1.0.x)
Schema Wasm             : @prisma/prisma-schema-wasm 5.14.0-25.e9771e62de70f79a5e1c604a2d7c8e2a0a874b48
Default Engines Hash    : e9771e62de70f79a5e1c604a2d7c8e2a0a874b48
Studio                  : 0.500.0
janpio commented 1 week ago

What happens to your Prisma schema when you run prisma db pull - does it update the expire_at definition, and then also fix the migration problem?