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

Changing the Column Type to Enum on a model throws an error #17930

Closed
nabhanh opened this issue Feb 15, 2023 · 3 comments
Closed

Changing the Column Type to Enum on a model throws an error #17930

nabhanh opened this issue Feb 15, 2023 · 3 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: cockroachdb topic: enum "type"/block `enum` topic: prisma db push CLI: prisma db push
Milestone

Comments

@nabhanh
Copy link

nabhanh commented Feb 15, 2023

Bug description

When changing a column type from String to Enum on a table (without any data) on CockroachDB throws a Error: db error: ERROR: type "{{name of the enum}}" does not exist

For more reference: https://prisma.slack.com/archives/CA491RJH0/p1675847178217929
export DEBUG=* output:

npx prisma db push                                                                                                                                   ─╯
  prisma:engines  binaries to download libquery-engine, migration-engine +0ms
  prisma:loadEnv  project root found at /Users/nabhanhanif/prisma_test/package.json +0ms
  prisma:tryLoadEnv  Environment variables loaded from /Users/nabhanhanif/prisma_test/.env +0ms
  prisma:getConfig  Using getConfig Wasm +0ms
  prisma:getConfig  config data retrieved without errors in getConfig Wasm +2ms
  prisma:loadEnv  project root found at /Users/nabhanhanif/prisma_test/package.json +42ms
  prisma:tryLoadEnv  Environment variables loaded from /Users/nabhanhanif/prisma_test/.env +40ms
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
  prisma:getConfig  Using getConfig Wasm +16ms
  prisma:getConfig  config data retrieved without errors in getConfig Wasm +3ms
Datasource "db": CockroachDB database "{{dbName}}", schema "public" at "{{dbUrl}}"
  prisma:getConfig  Using getConfig Wasm +23ms
  prisma:getConfig  config data retrieved without errors in getConfig Wasm +1ms
  prisma:migrateEngine:rpc  starting migration engine with binary: /Users/nabhanhanif/prisma_test/node_modules/@prisma/engines/migration-engine-darwin +0ms
  prisma:migrateEngine:rpc  SENDING RPC CALL {"id":1,"jsonrpc":"2.0","method":"schemaPush","params":{"force":false,"schema":"// This is your Prisma schema file,\n// learn more about it in the docs: https://pris.ly/d/prisma-schema\n\ngenerator client {\n  provider = \"prisma-client-js\"\n}\n\ndatasource db {\n  provider = \"cockroachdb\"\n  url      = env(\"DATABASE_URL\")\n}\n\nenum Gender {\n  MALE\n  FEMALE\n}\n\nmodel User {\n  userId String  @id @default(cuid())\n  name   String\n  gender String?\n}\n\nmodel Person {\n  personId  String  @id @default(cuid())\n  firstName String\n  lastName  String\n  gender    Gender?\n}\n"}} +5ms
  prisma:migrateEngine:stderr  {"timestamp":"2023-02-15T09:03:57.128765Z","level":"INFO","fields":{"message":"Starting migration engine RPC server","git_hash":"aead147aa326ccb985dcfed5b065b4fdabd44b19"},"target":"migration_engine"} +0ms
  prisma:migrateEngine:rpc  {
  jsonrpc: '2.0',
  error: {
    code: 4466,
    message: 'An error happened. Check the data field for details.',
    data: {
      is_panic: false,
      message: 'db error: ERROR: type "gender" does not exist\n' +
        '   0: sql_migration_connector::apply_migration::migration_step\n' +
        '           with step=AlterTable(AlterTable { table_ids: Pair { previous: TableId(0), next: TableId(1) }, changes: [AddColumn { column_id: TableColumnId(6), has_virtual_default: false }] })\n' +
        '             at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:21\n' +
        '   1: sql_migration_connector::apply_migration::apply_migration\n' +
        '             at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:10\n' +
        '   2: migration_core::state::SchemaPush\n' +
        '             at migration-engine/core/src/state.rs:433',
      backtrace: null
    }
  },
  id: 1
} +1s
Error: Error: db error: ERROR: type "gender" does not exist
   0: sql_migration_connector::apply_migration::migration_step
           with step=AlterTable(AlterTable { table_ids: Pair { previous: TableId(0), next: TableId(1) }, changes: [AddColumn { column_id: TableColumnId(6), has_virtual_default: false }] })
             at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:21
   1: sql_migration_connector::apply_migration::apply_migration
             at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:10
   2: migration_core::state::SchemaPush
             at migration-engine/core/src/state.rs:433

    at Object.<anonymous> (/Users/nabhanhanif/prisma_test/node_modules/prisma/build/index.js:96730:25)
    at MigrateEngine.handleResponse (/Users/nabhanhanif/prisma_test/node_modules/prisma/build/index.js:96581:36)
    at LineStream2.<anonymous> (/Users/nabhanhanif/prisma_test/node_modules/prisma/build/index.js:96681:16)
    at LineStream2.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at LineStream2._pushBuffer (/Users/nabhanhanif/prisma_test/node_modules/prisma/build/index.js:96436:17)
    at LineStream2._transform (/Users/nabhanhanif/prisma_test/node_modules/prisma/build/index.js:96430:8)
    at Transform._write (node:internal/streams/transform:175:8)

How to reproduce

Videos to Reproduce:
https://prisma.slack.com/archives/CA491RJH0/p1676288645369239?thread_ts=1675847178.217929&cid=CA491RJH0

  1. Run npx prisma db push on the given prisma.schema file
  2. Change the gender column on model User from String? to Gender? enum
  3. Run npx prisma db push
  4. See
npx prisma db push                                                                                                                                                                                                        
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": CockroachDB database "{{databaseName}}", schema "public" at "{{dbUrl}}"
Error: db error: ERROR: type "gender" does not exist
   0: sql_migration_connector::apply_migration::migration_step
           with step=AlterTable(AlterTable { table_ids: Pair { previous: TableId(24), next: TableId(12) }, changes: [AddColumn { column_id: TableColumnId(110), has_virtual_default: false }] })
             at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:21
   1: sql_migration_connector::apply_migration::apply_migration
             at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:10
   2: migration_core::state::SchemaPush
             at migration-engine/core/src/state.rs:433

Expected behavior

Expect the User model to alter the column datatype from String? to Gender? without any errors

Prisma information

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

datasource db {
  provider = "cockroachdb"
  url      = env("DATABASE_URL")
}

enum Gender {
  MALE
  FEMALE
}

model User {
  userId String  @id @default(cuid())
  name   String
  gender String?
}

model Person {
  personId  String  @id @default(cuid())
  firstName String
  lastName  String
  gender    Gender?
}
// Add your code using Prisma Client

Environment & setup

  • OS: MacOS (Ventura 13.1)
  • Database: CockroachDB v22.2.1 (Postgres v14.6)
  • Node.js version: v18.11.0

Prisma Version

prisma                  : 4.10.1
@prisma/client          : 4.10.1
Current platform        : darwin
Query Engine (Node-API) : libquery-engine aead147aa326ccb985dcfed5b065b4fdabd44b19 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli aead147aa326ccb985dcfed5b065b4fdabd44b19 (at node_modules/@prisma/engines/migration-engine-darwin)
Format Wasm             : @prisma/prisma-fmt-wasm 4.10.1-1.80b351cc7c06d352abe81be19b8a89e9c6b7c110
Default Engines Hash    : aead147aa326ccb985dcfed5b065b4fdabd44b19
Studio                  : 0.481.0
@nabhanh nabhanh added the kind/bug A reported bug. label Feb 15, 2023
@mcbethio mcbethio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/schema Issue for team Schema. topic: prisma db push CLI: prisma db push topic: enum "type"/block `enum` labels Feb 15, 2023
@janpio
Copy link
Member

janpio commented Feb 17, 2023

I can confirm this crash with Cockroach 22.2:

➜  /workspace git:(master) ✗ npx prisma db push
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": CockroachDB database "mydb", schema "public" at "localhost:26257"

CockroachDB database mydb created at localhost:26257

🚀  Your database is now in sync with your Prisma schema. Done in 332ms

✔ Generated Prisma Client (4.10.1 | library) to ./node_modules/@prisma/client in 243ms

➜  /workspace git:(master) ✗ npx prisma db push
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": CockroachDB database "mydb", schema "public" at "localhost:26257"
Error: db error: ERROR: type "gender" does not exist
   0: sql_migration_connector::apply_migration::migration_step
           with step=AlterTable(AlterTable { table_ids: Pair { previous: TableId(1), next: TableId(0) }, changes: [DropAndRecreateColumn { column_id: Pair { previous: TableColumnId(6), next: TableColumnId(2) }, changes: ColumnChanges { type_change: Some(NotCastable), changes: BitFlags<ColumnChange>(0b100, TypeChanged) } }] })
             at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:21
   1: sql_migration_connector::apply_migration::apply_migration
             at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:10
   2: migration_core::state::SchemaPush
             at migration-engine/core/src/state.rs:433

This works fine on 22.1

(Codesandbox I used for reproduction: https://codesandbox.io/p/sandbox/quiet-architecture-9yo5dq)

@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 Feb 17, 2023
@janpio
Copy link
Member

janpio commented Feb 17, 2023

This is most probably caused by this: #16909

@nabhanh
Copy link
Author

nabhanh commented Mar 3, 2023

@janpio Can confirm that this now works as expected with v4.12.0-dev.5 👍

@janpio janpio added this to the 4.12.0 milestone Mar 3, 2023
@janpio janpio closed this as completed Mar 3, 2023
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: cockroachdb topic: enum "type"/block `enum` topic: prisma db push CLI: prisma db push
Projects
None yet
Development

No branches or pull requests

3 participants