question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generates alter table statements for columns that haven't changed

See original GitHub issue

Bug description

Prisma generates alter table statements every time a migration is created (–create only) or applied prisma migrate dev when a model attribute contains dbgenerated("myschema.uuid_generate_v4()")

How to reproduce

  1. Create the following schema:
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = ""
}

model Account {
  id                  String  @id @default(dbgenerated("myschema.uuid_generate_v4()")) @db.Uuid

  @@map(name: "accounts")
}

model Product {
  id          String          @id @default(dbgenerated("myschema.uuid_generate_v4()")) @db.Uuid

  @@map(name: "products")
}
  1. Run prisma migrate dev --create-only.
  2. Manually update the migration to include: CREATE EXTENSION IF NOT EXISTS "uuid-ossp" SCHEMA myschema VERSION "1.1";.
  3. Apply migration by running prisma migrate dev.
  4. Prisma then generates a another migration.sql file with the following content:
-- AlterTable
ALTER TABLE "accounts" ALTER COLUMN "id" SET DEFAULT myschema.uuid_generate_v4();

-- AlterTable
ALTER TABLE "products" ALTER COLUMN "id" SET DEFAULT myschema.uuid_generate_v4();
  1. Add name attribute for the account model
model Account {
  id                  String  @id @default(dbgenerated("myschema.uuid_generate_v4()")) @db.Uuid
  name        String          @db.VarChar(50)
  
  @@map(name: "accounts")
}
  1. Apply the migration by running prisma migrate dev.
  2. Prisma generates again the alter for the id attributes
-- AlterTable
ALTER TABLE "accounts" ADD COLUMN     "name" VARCHAR(50) NOT NULL,
ALTER COLUMN "id" SET DEFAULT myschema.uuid_generate_v4();

-- AlterTable
ALTER TABLE "products" ALTER COLUMN "id" SET DEFAULT myschema.uuid_generate_v4();

Expected behavior

Not generate alter statements for the id columns since those haven’t change.

Prisma information

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

datasource db {
  provider = "postgresql"
  url      = ""
}

model Account {
  id                  String  @id @default(dbgenerated("myschema.uuid_generate_v4()")) @db.Uuid

  @@map(name: "accounts")
}

model Product {
  id          String          @id @default(dbgenerated("myschema.uuid_generate_v4()")) @db.Uuid

  @@map(name: "products")
}

Environment & setup

  • OS: Mac OS Big Sur 11.4
  • Database: PostgreSQL 13.2
  • Node.js version: v12.20.0

Prisma Version

prisma               : 2.24.1
@prisma/client       : Not found
Current platform     : darwin
Query Engine         : query-engine 18095475d5ee64536e2f93995e48ad800737a9e4 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 18095475d5ee64536e2f93995e48ad800737a9e4 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 18095475d5ee64536e2f93995e48ad800737a9e4 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 18095475d5ee64536e2f93995e48ad800737a9e4 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 18095475d5ee64536e2f93995e48ad800737a9e4
Studio               : 0.397.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tomhoulecommented, Jul 5, 2021

We need to find a user-friendlier way to deal with this, but glad the workaround worked for you 😃

2reactions
ericArbourcommented, Jul 5, 2021

@tomhoule Ah thank you. I peaked at the sql file and this is specifically what prisma generated: date_trunc('month'::text, now()). Updating my attribute to @default(dbgenerated("date_trunc('month'::text, now())")) has fixed the issue for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Alter Columns or Generate Alter Scripts by ... - YouTube
SQL Server / TSQL Tutorial Scenario: How to Alter Columns or Generate Alter Scripts by using GUI in SQL ServerYou have created a...
Read more >
ALTER TABLE: a SQL statement for altering a database table
Existing tables can be altered with an ALTER TABLE statement. An ALTER TABLE ... That column currently does not have a NOT NULL...
Read more >
How to Alter Columns or Generate Alter Scripts by using GUI ...
Step 1: Go to Database and then go to Tables tab. Once you see your required table, Right Click and hit Design. ......
Read more >
Create table or only add changed/new columns - Stack Overflow
I have several tables which are worked on within a development environment, then moved to production. If they don't already exist in production, ......
Read more >
Generate DDL - change is a new column and I want to ...
Here I would like to have the generate be aware the it needs only to generate an alter table add column and not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found