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.

Error while creating a new migration with a shadow database and some enums

See original GitHub issue

Bug description

I created my first migration in my dev database using the migrate feature. It went well but when I try to create a new migration it fails while doing a soft reset of my shadow database.

It tries to delete the custom types (regular Prisma enums) before droppping the tables using it.

How to reproduce

prisma migrate dev --preview-feature

Expected behavior

It should not crash.

Prisma information

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

datasource postgres {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}

model User {
  createdAt            DateTime              @default(now())
  createdById          String                @default("-1")
  email                String                @unique
  firstName            String
  id                   String                @id @default(uuid())
  language             Language              @default(FR)
  lastName             String
  updatedAt            DateTime              @updatedAt
  adminRole            AdminRole?
}

model AdminRole {
  createdAt   DateTime      @default(now())
  createdById String        @default("-1")
  type        AdminRoleType
  updatedAt   DateTime      @updatedAt
  updatedById String?
  userId      String        @id
  user        User          @relation(fields: [userId], references: [id])
}

enum Language {
  EN
  FR
}

enum AdminRoleType {
  SUPER
  NATIONAL
  LOCAL
}
> prisma "migrate" "dev" "--preview-feature"

  prisma:tryLoadEnv Environment variables not found at null +0ms
  prisma:tryLoadEnv Environment variables loaded from ./prisma/.env +1ms
Environment variables loaded from prisma/.env
Prisma schema loaded from prisma/schema.prisma
Datasource "postgres": PostgreSQL database "xxxx", schema "public" at "xxxxxx"

  prisma:migrateEngine:rpc starting migration engine with binary: /Users/me/dev/backend/packages/models/node_modules/@prisma/engines/migration-engine-darwin +0ms
  prisma:migrateEngine:rpc SENDING RPC CALL {"id":1,"jsonrpc":"2.0","method":"devDiagnostic","params":{"migrationsDirectoryPath":"/Users/me/dev/backend/packages/models/prisma/migrations"}} +5ms
  prisma:migrateEngine:stderr Feb 25 14:07:31.574  INFO migration_engine: Starting migration engine RPC server git_hash="3c463ebd78b1d21d8fdacdd27899e280cf686223" +0ms
  prisma:migrateEngine:stderr Feb 25 14:07:31.647  INFO quaint::single: Starting a postgresql connection. +73ms
  prisma:migrateEngine:stderr Feb 25 14:07:31.746  INFO DevDiagnostic:calculate_drift:sql_schema_from_migration_history: quaint::single: Starting a postgresql connection. +98ms
  prisma:migrateEngine:stderr Feb 25 14:07:31.746  INFO DevDiagnostic:calculate_drift:sql_schema_from_migration_history: sql_migration_connector::flavour::postgres: Connecting to user-provided shadow database at postgresql://xxxxxx +0ms
  prisma:migrateEngine:stderr Feb 25 14:07:31.753  INFO DevDiagnostic:calculate_drift:sql_schema_from_migration_history:best_effort_reset{connection=Connection(Postgres(PostgresUrl { url: Url { scheme: "postgresql", host: Some(Domain("xxxxxx")), port: Some(xxxxx), path: "xxxxxx", query: None, fragment: None }, query_params: PostgresUrlQueryParams { ssl_params: SslParams { certificate_file: None, identity_file: None, identity_password: <HIDDEN>, ssl_accept_mode: AcceptInvalidCerts }, connection_limit: None, schema: "public", ssl_mode: Prefer, pg_bouncer: false, host: None, socket_timeout: None, connect_timeout: Some(5s), pool_timeout: Some(5s), statement_cache_size: 500 } }))}: sql_migration_connector: Attempting best_effort_reset +7ms
  prisma:migrateEngine:rpc {
  prisma:migrateEngine:rpc   jsonrpc: '2.0',
  prisma:migrateEngine:rpc   error: {
  prisma:migrateEngine:rpc     code: 4466,
  prisma:migrateEngine:rpc     message: 'An error happened. Check the data field for details.',
  prisma:migrateEngine:rpc     data: {
  prisma:migrateEngine:rpc       is_panic: false,
  prisma:migrateEngine:rpc       message: 'Database error: Error querying the database: db error: ERROR: constraint "Activity_iconId_fkey" of relation "Activity" does not exist\n' +
  prisma:migrateEngine:rpc         '   0: sql_migration_connector::sql_database_step_applier::apply_migration\n' +
  prisma:migrateEngine:rpc         '             at migration-engine/connectors/sql-migration-connector/src/sql_database_step_applier.rs:14\n' +
  prisma:migrateEngine:rpc         '   1: sql_migration_connector::best_effort_reset\n' +
  prisma:migrateEngine:rpc         '           with connection=Connection(Postgres(PostgresUrl { url: Url { scheme: "postgresql", host: Some(Domain("xxxxxx")), port: Some(xxxxxx), path: "xxxxxxx", query: None, fragment: None }, query_params: PostgresUrlQueryParams { ssl_params: SslParams { certificate_file: None, identity_file: None, identity_password: <HIDDEN>, ssl_accept_mode: AcceptInvalidCerts }, connection_limit: None, schema: "public", ssl_mode: Prefer, pg_bouncer: false, host: None, socket_timeout: None, connect_timeout: Some(5s), pool_timeout: Some(5s), statement_cache_size: 500 } }))\n' +
  prisma:migrateEngine:rpc         '             at migration-engine/connectors/sql-migration-connector/src/lib.rs:104\n' +
  prisma:migrateEngine:rpc         '   2: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history\n' +
  prisma:migrateEngine:rpc         '             at migration-engine/connectors/sql-migration-connector/src/flavour/postgres.rs:270\n' +
  prisma:migrateEngine:rpc         '   3: sql_migration_connector::sql_database_migration_inferrer::calculate_drift\n' +
  prisma:migrateEngine:rpc         '             at migration-engine/connectors/sql-migration-connector/src/sql_database_migration_inferrer.rs:40\n' +
  prisma:migrateEngine:rpc         '   4: migration_core::api::DevDiagnostic\n' +
  prisma:migrateEngine:rpc         '             at migration-engine/core/src/api.rs:79',
  prisma:migrateEngine:rpc       backtrace: null
  prisma:migrateEngine:rpc     }
  prisma:migrateEngine:rpc   },
  prisma:migrateEngine:rpc   id: 1
  prisma:migrateEngine:rpc } +373ms
Error: Error: Database error: Error querying the database: db error: ERROR: constraint "Activity_iconId_fkey" of relation "Activity" does not exist
   0: sql_migration_connector::sql_database_step_applier::apply_migration
             at migration-engine/connectors/sql-migration-connector/src/sql_database_step_applier.rs:14
   1: sql_migration_connector::best_effort_reset
           with connection=Connection(Postgres(PostgresUrl { url: Url { scheme: "postgresql", host: Some(Domain("xxxxxxx")), port: Some(xxxxx), path: "xxxxxxx", query: None, fragment: None }, query_params: PostgresUrlQueryParams { ssl_params: SslParams { certificate_file: None, identity_file: None, identity_password: <HIDDEN>, ssl_accept_mode: AcceptInvalidCerts }, connection_limit: None, schema: "public", ssl_mode: Prefer, pg_bouncer: false, host: None, socket_timeout: None, connect_timeout: Some(5s), pool_timeout: Some(5s), statement_cache_size: 500 } }))
             at migration-engine/connectors/sql-migration-connector/src/lib.rs:104
   2: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history
             at migration-engine/connectors/sql-migration-connector/src/flavour/postgres.rs:270
   3: sql_migration_connector::sql_database_migration_inferrer::calculate_drift
             at migration-engine/connectors/sql-migration-connector/src/sql_database_migration_inferrer.rs:40
   4: migration_core::api::DevDiagnostic
             at migration-engine/core/src/api.rs:79

    at Object.<anonymous> (/Users/me/dev/backend/packages/models/node_modules/prisma/build/index.js:54907:26)
    at MigrateEngine.handleResponse (/Users/me/dev/backend/packages/models/node_modules/prisma/build/index.js:54780:38)
    at LineStream.<anonymous> (/Users/me/dev/backend/packages/models/node_modules/prisma/build/index.js:54865:18)
    at LineStream.emit (node:events:378:20)
    at LineStream.EventEmitter.emit (node:domain:470:12)
    at addChunk (node:internal/streams/readable:313:12)
    at readableAddChunk (node:internal/streams/readable:288:9)
    at LineStream.Readable.push (node:internal/streams/readable:227:10)
    at LineStream._pushBuffer (/Users/me/dev/backend/packages/models/node_modules/prisma/build/index.js:54617:19)
    at LineStream._transform (/Users/me/dev/backend/packages/models/node_modules/prisma/build/index.js:54611:10)

Environment & setup

  • OS: macOS
  • Database: PostgreSQL
  • Node.js version: v15.9.0
  • Prisma version:
prisma               : 2.17.0
@prisma/client       : 2.17.0
Current platform     : darwin
Query Engine         : query-engine 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Studio               : 0.353.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tomhoulecommented, Feb 25, 2021

Thanks for the issue, it does look like a bug, we’ll look into it!

0reactions
janpiocommented, Sep 9, 2021

Thoughts during a workshop:

  • Theory: Caused by database having a different constraint name than what Migrate created in the migration SQL based on the data model, making the migration fail.
  • Counter point: Seems to be in the shadow database part. That starts from empty, so there should be no way to have drift there.
  • Again counter point: Migration SQL could have been hand written or modified?

Can you tell us a bit more about this @julien1619?

Read more comments on GitHub >

github_iconTop Results From Across the Web

About the shadow database - Prisma
When you run prisma migrate dev to create a new migration, Prisma Migrate uses the shadow database to: Detect schema drift, which means...
Read more >
Error when migrating models to database Prisma
I could fix this issue by creating another DB in Heroku as my shadow DB. Then in the schema.prisma add the code bellow:...
Read more >
graphile-migrate - npm
Start using graphile-migrate in your project by running `npm i ... Create your database role (if desired), database and shadow database:.
Read more >
Breaking changes in EF Core 6.0 - Microsoft Learn
Unknown enum string values in the database are not converted to the ... properties to exist on an owned type, EF will still...
Read more >
Rails Enums with MySQL or Postgres - Daniela Baron
Database Migration. The first step to implement an enum in your Rails project will be to generate a migration to add the column....
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