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.

Prisma v4 breaks support for empty `dbgenerated()` - invalid migration created with no schema change

See original GitHub issue

Bug description

Using Postgres generated columns via @default(dbgenerated()) with custom Postgres GENERATED AWAYS AS ... STORED SQL in migration worked just fine through Prisma v3; breaks with Prisma 4.0.0 onwards.

Reproduction repo: https://github.com/andyjy/prisma-dbgenerated-bug-repro

Reproduction details:

schema.prisma:

model table {
    id String @id
    hereBeDragons String @default(dbgenerated())
}

…with custom migration to use Postgres generated field:

migration.sql:

CREATE TABLE "table" (
    ...

    -- manually edited migration to make column generated
    "hereBeDragons" TEXT NOT NULL GENERATED ALWAYS AS (
'this row ID is: '::text || "id") STORED,

    ...

Worked just fine through prisma 3.15.2; prisma 4.0.0 onwards produces invalid migration with no schema change that attempts to DROP DEFAULT (which we don’t want to do, and fails since Postgres generated columns require use of DROP EXPRESSION if we were actually to want to convert the generated column to a normal one).

...invalid_failed_migration/migration.sql:

ALTER TABLE "table" ALTER COLUMN "hereBeDragons" DROP DEFAULT;

(unwanted new migration fails with error:)

Database error code: 42601

Database error:
ERROR: column "hereBeDragons" of relation "table" is a generated column
HINT: Use ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION instead.

How to reproduce

Reproduction repo: https://github.com/andyjy/prisma-dbgenerated-bug-repro

Expected behavior

Prisma 4 continues to support this schema and migration history with no attempt to generate a new migration dropping the default value for the column.

Prisma information

Schema above + in reproduction repo

Environment & setup

  • OS: MacOS
  • Database: PostgreSQL
  • Node.js version: v16.16.0

Prisma Version

works up to 3.15.2, fails since 4.0.0 (tested up to 4.2.1)

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
andyjycommented, Sep 28, 2022

now enjoying the fix in 4.4.0 - thanks @tomhoule 🙏

2reactions
tomhoulecommented, Aug 30, 2022

Due to CI issues, the version of prisma-engines that was released in 4.3.0 is a bit older and does not contain this fix, sadly. So it is merged on main, it will be on 4.4.0 dev versions and in the 4.4.0 release, but it won’t be in 4.3.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma schema API (Reference)
Describes which generator to use. This can point to a file that implements a generator or specify a built-in generator directly. output, No,...
Read more >
Migration troubleshooting in development - Prisma
This guide describes how to resolve issues with Prisma Migrate in a development environment, which often involves resetting your database.
Read more >
Upgrade to Prisma 4
This section gives an overview of breaking changes in Prisma 4, grouped under general changes that affect both the Prisma Schema and the...
Read more >
Prisma Migrate limitations and known issues
Prisma Migrate does not currently support the MongoDB connector. ... new initial migration that goes from an empty database to your current schema.prisma...
Read more >
Customize a migration file - Prisma
Make a schema change that requires custom SQL (for example, to preserve existing data). Create a draft migration using: $npx prisma migrate dev...
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