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

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

Closed
andyjy opened this issue Aug 14, 2022 · 13 comments · Fixed by prisma/prisma-engines#3153
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: dbgenerated topic: generated columns
Milestone

Comments

@andyjy
Copy link
Contributor

andyjy commented Aug 14, 2022

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)

@andyjy andyjy added the kind/bug A reported bug. label Aug 14, 2022
@do4gr do4gr added team/schema Issue for team Schema. bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Aug 15, 2022
@janpio
Copy link
Member

janpio commented Aug 15, 2022

I can confirm that on commit 6c36a0f5565a9759ad887473637980c550ba10a7 I can migrate deploy and then run migrate dev with it reporting Already in sync, no schema change or pending migration was found..

I can unfortunately also confirm that upgrading to the newest Prisma version then leads to migrate dev creating this migration, as described:

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

@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 Aug 15, 2022
@KunJakob
Copy link

We are also running into this error which has been a big dealbreaker for us, as it was a perfect stop-gap before computed fields are officially supported in Prisma.

@tomhoule
Copy link
Contributor

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.

@1Vlod
Copy link

1Vlod commented Sep 23, 2022

@tomhoule Hi, could you please let us know the expected release date for version 4.4.0?

I just faced with this one and it blocked me. As I understood there are only workarounds: use version lower than 4.x.x or wait for release 4.4.0 version, am I right?

@tomhoule
Copy link
Contributor

4.4.0 is scheduled for next Tuesday (Sept. 27). And yes this is a bad regression. There is another workaround: picking a dev release version on npm. They are built on a rolling basis so the fix is already in.

@andyjy
Copy link
Contributor Author

andyjy commented Sep 23, 2022

@1Vlod the other workaround is to edit the failing migration file to remove the offending lines and reapply using prisma migrate deploy or prisma migrate reset.

@andyjy
Copy link
Contributor Author

andyjy commented Sep 28, 2022

now enjoying the fix in 4.4.0 - thanks @tomhoule 🙏

@jkcorrea
Copy link

jkcorrea commented Oct 3, 2022

@andyjy @tomhoule This seems to still not be working for me (tried v4.4 and v4.5-dev). Am I doing something wrong here?

Prisma schema:

model Event {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  name  String
  start DateTime
  end   DateTime
  fts   Unsupported("tsvector")? @default(dbgenerated())

  @@index([fts], type: Gin)
  @@map("event")
}

Modified SQL migration:

ALTER TABLE "event" ADD COLUMN "fts" tsvector GENERATED ALWAYS AS (
  to_tsvector('english',
    id::text -- add the cuid to the vector
    || ' ' || COALESCE("name", '') -- concat more fields (or empty string if null)
  )
) STORED;
CREATE INDEX event_fts_idx ON "event" using gin (fts); -- generate the index

Prisma migrate then generates and tries to run this migration:

-- AlterTable
ALTER TABLE "event" ALTER COLUMN "fts" DROP DEFAULT;

Which will fail since the column is generated

@andyjy
Copy link
Contributor Author

andyjy commented Oct 4, 2022

@jkcorrea you might find it useful to fork my minimum reproducible example repo and verify whether you still get this error if you swap out my example schema for yours: https://github.com/andyjy/prisma-dbgenerated-bug-repro

Did you remember to update the prisma devDependency to v4.4.0 in addition to @prisma/client?

@jkcorrea
Copy link

jkcorrea commented Oct 4, 2022

@jkcorrea you might find it useful to fork my minimum reproducible example repo and verify whether you still get this error if you swap out my example schema for yours: https://github.com/andyjy/prisma-dbgenerated-bug-repro

Did you remember to update the prisma devDependency to v4.4.0 in addition to @prisma/client?

That was very helpful, thanks @andyjy. Was able to use your minimum repro to figure out that it's due to using an Unsupported() type column:

https://github.com/jkcorrea/prisma-unsupported-dbgenerated-bug

Happening in v4.4.0 and v4.5 & for both prisma and @prisma/client

@tomhoule
Copy link
Contributor

tomhoule commented Oct 4, 2022

@jkcorrea thanks for putting the effort in writing a reproduction repo for this. Please open a new issue, since it's a different problem, otherwise it will be forgotten if it's only a comment in a closed issue.

@jkcorrea
Copy link

jkcorrea commented Oct 4, 2022

@jkcorrea thanks for putting the effort in writing a reproduction repo for this. Please open a new issue, since it's a different problem, otherwise it will be forgotten if it's only a comment in a closed issue.

Was in the process of doing just that, thanks :)

#15654

@tomhoule
Copy link
Contributor

tomhoule commented Oct 4, 2022

awesome, thanks!

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: dbgenerated topic: generated columns
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants