Repository navigation
Description
Bug description
I created a new project from scratch then I added @id @default(dbgenerated("public.gen_random_uuid()")) one of id field.The database was created on PostgreSql.
After that, I want to add a new column to our existing schema.prisma .When I run npx prisma migrate dev It returns this error Migration 20210405204104_202005042340 failed to apply cleanly to a temporary database.
We are using custom uuid which describe like this => @id @default(dbgenerated("public.gen_random_uuid()")) @db.Uuid
How to reproduce
Steps to reproduce the behavior:
- Go to schema.prisma
- Add relation
model Author {
id String @id @default(dbgenerated("public.gen_random_uuid()")) @db.Uuid
name String
confirmedBy User? @relation("User", fields: [confirmedById], references: [id])
confirmedById String? @db.Uuid
}
- Run npx prisma migrate dev
- See error
Error: P3006
Migration `20210405204104_202005042340` failed to apply cleanly to a temporary database.
Error:
Database error
Error querying the database: db error: ERROR: function public.gen_random_uuid() does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
0: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history
at migration-engine/connectors/sql-migration-connector/src/flavour/postgres.rs:278
1: sql_migration_connector::sql_database_migration_inferrer::validate_migrations
at migration-engine/connectors/sql-migration-connector/src/sql_database_migration_inferrer.rs:88
2: migration_core::api::DevDiagnostic
at migration-engine/core/src/api.rs:95
Expected behavior
We were waiting for the database to be updated successfully migrated
Prisma information
model User {
id String @id @default(dbgenerated("public.gen_random_uuid()")) @db.Uuid
email String?
Author Author[] @relation("User")
}
model Author {
id String @id @default(dbgenerated("public.gen_random_uuid()")) @db.Uuid
name String
confirmedBy User? @relation("User", fields: [confirmedById], references: [id])
confirmedById String? @db.Uuid
}
Environment & setup
- OS: Mac OS
- Database: PostgreSQL
- Node.js version: v14.16.0
- Prisma version: 2.20.1

Activity
janpio commentedon Apr 8, 2021
You probably need to create that uuid function on the temporary/shadow database as well - it is completely empty and does not have any content or functions present.
You can do so by running
npx prisma migrate dev --create-only, then modify themigrations.sqlfile to include the SQL for that, then runnpx prisma migrate devagain to apply the migration.erdoganabaci commentedon Apr 9, 2021
firstly thanks for replying @janpio can you give more details how can i modify migrations.sql file under the prisma folder ?
also i run npx prisma migrate dev --create-only this command still same error
tomhoule commentedon Apr 9, 2021
All migrations are applied to the shadow database when you use
--create-only, this is where it fails. The reason why the migration fails is because you have toCREATE EXTENSION ....on the shadow database the same way you did on the dev database. You can achieve this by adding theCREATE EXTENSIONto your first migration or another migration towards the beginning of your migrations history.tomhoule commentedon Apr 9, 2021
We have an internal issue about documenting this better.
janpio commentedon Apr 9, 2021
Ah ok, so I missed that any existing migration will also require this and that
--create-onlywill already apply those @erdoganabaci - sorry about that. So you should be able to edit your first migration as @tomhoule said to make sure the function exists and the migrations should apply cleanly withnpx prisma migrate dev👍pantharshit00 commentedon Apr 12, 2021
Full SQL for creating the postgres uuid extension:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";erdoganabaci commentedon Apr 12, 2021
@pantharshit00 where can I copy in the migration sql ?
Here my first migration.sql starting like this
janpio commentedon Apr 12, 2021
At the top optimally, so it will be available when the
CREATE TABLEis executed.erdoganabaci commentedon Apr 12, 2021
@janpio @pantharshit00 still same error I added top of the place on the migration.sql file but same error even I change the database url I used fresh db and both command
npx prisma migrate dev --create-onlyandnpx prisma migrate devHere there is screenshot
janpio commentedon Apr 12, 2021
Last idea I have would be to remove the
public.from line 18 (and other occurrences). With the extension enabled this should really be available globally I think. If that's not it, I can ask our colleagues working on Migrate.tomhoule commentedon Apr 12, 2021
@erdoganabaci not sure, but I think the
CREATE EXTENSIONmight need to be in a separate migration file that is executed first, before you can use it.erdoganabaci commentedon Apr 12, 2021
@tomhoule @janpio @pantharshit00 I tried all possibilities one of them is to create a new initial migration.sql include these commands
After it created with public.gen_random_uuid() then the second time i changed schema.prisma then it returns the same error.
After that, I deleted public function name in the prisma.schema gen_random_uuid() only this command applied.
Then return the same error, unfortunately.
tomhoule commentedon Apr 13, 2021
@erdoganabaci — The migration will be executed after the other migration — the execution order is the alphanumerical order of the migration directories (this is the reason for the timestamps). What happens if you rename the
initial_migrationfolder to0000_initial_migration?32 remaining items
kokokenada commentedon Jun 15, 2022
This problem has been a major complication as I attempt to change from Hasura migrations to prisma migrate. The work around of adding
CREATE EXTENSION pgcrypto;as described by https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate/enable-native-database-functions (even as a separate initial migration) simply does not work when you runprisma migrate dev. The only workaround I have found so far is to use a shadow database and manually add the extension there.skplunkerin commentedon Mar 4, 2023
Just ran into this same issue, but in my case it was caused by
npx prisma db pullnot being smart enough to also pull down my existing enabled extensions (sadly the command is only smart enough to pull down the DB tables).Then once I ran
npx prisma migrate dev --name initto get the initial migration files created to match my current DB setup, the command removes already installed extensions and then fails with the same error ofERROR: function public.gen_random_uuid() does not exist.I would expect either (or both)
npx prisma db pullandnpx prisma migrate dev --name initcommands to be smart enough to pull down installed extensions and add those to the initialized migration files automatically.janpio commentedon Mar 4, 2023
To pull down the extensions, you for now have to manually enable the preview feature first: https://www.prisma.io/docs/concepts/components/prisma-schema/postgresql-extensions#how-to-enable-the-postgresqlextensions-preview-feature With that it should work. (If will generally work as soon as this feature leaves the preview feature stage and is generally available - but we need more feedback to make that decision.)
skplunkerin commentedon Mar 6, 2023
@janpio thanks, I didn't know this was something I could enable. I'll give this a try 💪
skplunkerin commentedon Mar 7, 2023
Testing this, it doesn't pull down any extensions. Do you know where I can share my feedback on this?
janpio commentedon Mar 10, 2023
Open a new issue please and provide all the information it asks for. Thanks.
victorsfranco commentedon Mar 11, 2024
I did this on my initial migration file, and it solved my problem. Thank you bro!
Ps: the only thing that I did different, its the extension created, where I created the extension "pgcrypto" instead of "uuid-ossp"
kiwavi commentedon Dec 26, 2024
Worked for me.