"Database error: Error querying the database: db error: ERROR: type "citext" does not exist" but citext extension enabled
See original GitHub issueBug description
Migrations after the first one fail due to type citext not existing, but I’ve confirmed numerous times and ensured that the citext extension is enabled on my database before I run the prisma migrate command. After running the prisma migrate command and seeing the error, I can confirm that the extension is removed from my database.
I’m assuming something funky is happening with prisma/shadow DB stuff?
How to reproduce
- Create a DB via
psqland runCREATE EXTENSION citext;on the DB. - Ensure your prisma folder has no migrations folder (so you’re starting brand new)
- Create a
prisma.schemawith a model that has a citext attribute likeemail String @unique @db.Citext. - Run
prisma migrate dev --preview-feature - Since this is the first migration, it should work.
- Edit your
prisma.schema- maybe by adding an attribute likename Stringto a model. - Run
prisma migrate dev --preview-feature - You should see the following error output:
prisma:tryLoadEnv Environment variables loaded from /Users/david/Code/project/packages/server2/.env +0ms
prisma:tryLoadEnv Environment variables loaded from ./prisma/.env +3ms
Environment variables loaded from .env
Environment variables loaded from prisma/.env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "project", schema "public" at "localhost:5432"
prisma:migrateEngine:rpc starting migration engine with binary: /Users/david/Code/project/packages/server2/node_modules/@prisma/engines/migration-engine-darwin +0ms
prisma:migrateEngine:rpc SENDING RPC CALL {"id":1,"jsonrpc":"2.0","method":"devDiagnostic","params":{"migrationsDirectoryPath":"/Users/david/Code/project/packages/server2/prisma/migrations"}} +3ms
prisma:migrateEngine:stderr Feb 21 15:05:30.427 INFO migration_engine: Starting migration engine RPC server git_hash="3c463ebd78b1d21d8fdacdd27899e280cf686223" +0ms
prisma:migrateEngine:stderr Feb 21 15:05:30.434 INFO quaint::single: Starting a postgresql connection. +5ms
prisma:migrateEngine:stderr Feb 21 15:05:30.733 INFO DevDiagnostic:calculate_drift:sql_schema_from_migration_history: quaint::single: Starting a postgresql connection. +299ms
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: 'Migration `20210221102106_hello_world` failed to apply cleanly to a temporary database. \n' +
prisma:migrateEngine:rpc 'Error:\n' +
prisma:migrateEngine:rpc 'Database error: Error querying the database: db error: ERROR: type "citext" does not exist\n' +
prisma:migrateEngine:rpc ' 0: 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 ' 1: 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 ' 2: migration_core::api::DevDiagnostic\n' +
prisma:migrateEngine:rpc ' at migration-engine/core/src/api.rs:79',
prisma:migrateEngine:rpc meta: [Object],
prisma:migrateEngine:rpc error_code: 'P3006'
prisma:migrateEngine:rpc }
prisma:migrateEngine:rpc },
prisma:migrateEngine:rpc id: 1
prisma:migrateEngine:rpc } +370ms
Error: Error: P3006
Migration `20210221102106_hello_world` failed to apply cleanly to a temporary database.
Error:
Database error: Error querying the database: db error: ERROR: type "citext" does not exist
0: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history
at migration-engine/connectors/sql-migration-connector/src/flavour/postgres.rs:270
1: sql_migration_connector::sql_database_migration_inferrer::calculate_drift
at migration-engine/connectors/sql-migration-connector/src/sql_database_migration_inferrer.rs:40
2: migration_core::api::DevDiagnostic
at migration-engine/core/src/api.rs:79
at Object.<anonymous> (/Users/david/Code/project/packages/server2/node_modules/prisma/build/index.js:54905:26)
at MigrateEngine.handleResponse (/Users/david/Code/project/packages/server2/node_modules/prisma/build/index.js:54780:38)
at LineStream.<anonymous> (/Users/david/Code/project/packages/server2/node_modules/prisma/build/index.js:54865:18)
at LineStream.emit (events.js:209:13)
at LineStream.EventEmitter.emit (domain.js:476:20)
at addChunk (_stream_readable.js:305:12)
at readableAddChunk (_stream_readable.js:286:11)
at LineStream.Readable.push (_stream_readable.js:220:10)
at LineStream.Transform.push (_stream_transform.js:150:32)
at LineStream._pushBuffer (/Users/david/Code/project/packages/server2/node_modules/prisma/build/index.js:54617:19)
- Head over to
psqland you’ll notice that the citext extension was removed from your DB.
Expected behavior
I expect the migration to succeed.
Prisma information
DEBUG="*" npx prisma migrate dev --preview-feature is the command I ran.
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
email String @unique @db.Citext
}
That schema should suffice.
Environment & setup
- OS: macOS Big Sur 11.1
- Database: PostgreSQL 12.3 on x86_64-apple-darwin19.4.0, compiled by Apple clang version 11.0.3 (clang-1103.0.32.59), 64-bit
- Node.js version: v12.10.0
- Prisma version: 2.17
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:
- Created 3 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Postgresql: ERROR: type "citext" does not exist - Stack Overflow
I have several databases and CREATE EXTENSION citext has to be run for each db to install the extension in that DB. You...
Read more >Documentation: 15: F.10. citext - PostgreSQL
The citext data type allows you to eliminate calls to lower in SQL queries, and allows a primary key to be case-insensitive. citext...
Read more >Postgres Extension - CITEXT for Case Insensitive Columns
Postgres has a column extension called CITEXT (stands for case-insensitive extension). This type allows one to query for the column in a ...
Read more >Unknown database type citext requested error when making ...
Hello! I am working on a migration that is supposed to make some existing fields nullable. Here is a snippet of the migration:...
Read more >How to represent PostgreSQL extensions in your ... - Prisma
However, there are the following differences: If you remove an extension from your schema but it is still activated on your database, Prisma...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You have to enable the extension from a migration for the shadow database to pick it up. So have a CREATE EXTENSION in your first migration, usually.
Keeping the issue open to see if there is any alternative we can think of, but so far I don’t consider this a bug.
Closing this there a new issue here https://github.com/prisma/prisma/issues/6944