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.

`ALTER TYPE` enum migrations fail in PostgreSQL

See original GitHub issue

Bug description

Using migrations from version 2.14, I updated an enum to add a few more values.

That generated the following migration.

-- AlterEnum
ALTER TYPE "account_type_enum" ADD VALUE 'Giving';
ALTER TYPE "account_type_enum" ADD VALUE 'Parent';
ALTER TYPE "account_type_enum" ADD VALUE 'Saving';
ALTER TYPE "account_type_enum" ADD VALUE 'Spending';

-- AlterTable
ALTER TABLE "account" ALTER COLUMN "type" SET DEFAULT E'Spending';

Unfortunately, when running this, I get the following error from Postgres.

Database error: Error querying the database: db error: ERROR: ALTER TYPE ... ADD cannot run inside a transaction block
   0: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history
             at migration-engine/connectors/sql-migration-connector/src/flavour/postgres.rs:174
   1: sql_migration_connector::sql_database_migration_inferrer::validate_migrations
             at migration-engine/connectors/sql-migration-connector/src/sql_database_migration_inferrer.rs:89
   2: migration_core::api::DiagnoseMigrationHistory
             at migration-engine/core/src/api.rs:109

I can confirm that reducing the migration to a single ALTER TYPE solves the issue.

How to reproduce

  1. Use Postgres
  2. Create an enum
  3. Add multiple new values
  4. Generate a migration
  5. Run the migration

Expected behavior

New enum values should be added.

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL 11
  • Node.js version: 4.15
  • Prisma version:
@prisma/cli          : 2.14.0
@prisma/client       : 2.14.0
Current platform     : darwin
Query Engine         : query-engine 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 5d491261d382a2a5ffdc71de17072b0e409f1cc1 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Studio               : 0.332.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
pimeyscommented, Feb 8, 2021

It seems that this restriction is not valid anymore for PostgreSQL versions higher than 11. On versions 12 and 13 I can successfully execute this migration.

3reactions
albertoperdomocommented, Feb 8, 2021

Created https://github.com/prisma/prisma/issues/5543 for us to add a comment with a warning, suggesting users of PostgreSQL 11 and earlier versions to only add one ENUM value at a time in each migration.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: ALTER TYPE - PostgreSQL
This form adds a new value to an enum type. The new value's place in the enum's ordering can be specified as being...
Read more >
Altering Enum type in Postgres gets 42883 error
1 Answer 1 · Using Postgres 12.3 and the error comes up using a migration through an Elixir app and through the application...
Read more >
[Solved]-Migration error: ENUM type already exists-postgresql
Since you already have a status enum type from your previous model, the solution is extremely easy and straightforward: just specify the type...
Read more >
Dealing with Enum Type in PostgreSQL - DEV Community ‍ ‍
How are you handling migrations where some of the enum values are removed? Existing data are using an enum value which should be...
Read more >
Upgrading PostgreSQL's Enum type with ... - Makimo
Furthermore if you are using an older version of PostgreSQL (<12) you will have to add an autocommit block every time when using...
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