Skip to content

Public UUID postgre migrate error !  #6471

Open

Description

@erdoganabaci

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.

Screen Shot 2021-04-08 at 22 09 13

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:

  1. Go to schema.prisma
  2. 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
}
  1. Run npx prisma migrate dev
  2. 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

  1. janpio commented on Apr 8, 2021

    @janpio
    Contributor

    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 the migrations.sql file to include the SQL for that, then run npx prisma migrate dev again to apply the migration.

  2. erdoganabaci commented on Apr 9, 2021

    @erdoganabaci
    Author

    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

    Screen Shot 2021-04-09 at 03 13 16

  3. tomhoule commented on Apr 9, 2021

    @tomhoule
    Contributor

    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 to CREATE EXTENSION .... on the shadow database the same way you did on the dev database. You can achieve this by adding the CREATE EXTENSION to your first migration or another migration towards the beginning of your migrations history.

  4. tomhoule commented on Apr 9, 2021

    @tomhoule
    Contributor

    We have an internal issue about documenting this better.

  5. janpio commented on Apr 9, 2021

    @janpio
    Contributor

    Ah ok, so I missed that any existing migration will also require this and that --create-only will 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 with npx prisma migrate dev 👍

  6. pantharshit00 commented on Apr 12, 2021

    @pantharshit00
    Contributor

    Full SQL for creating the postgres uuid extension:

    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
  7. erdoganabaci commented on Apr 12, 2021

    @erdoganabaci
    Author

    @pantharshit00 where can I copy in the migration sql ?

    Here my first migration.sql starting like this

    -- CreateEnum
    CREATE TYPE "StoreProvider" AS ENUM ('Amazon', 'Ebay', 'Walmart');
    
    -- CreateEnum
    CREATE TYPE "Currency" AS ENUM ('USD', 'EUR');
    
    -- CreateEnum
    CREATE TYPE "ProjectName" AS ENUM ('name', 'name1');
    
    -- CreateEnum
    CREATE TYPE "Status" AS ENUM ('Pending', 'Processing', 'Completed', 'Canceled', 'Failed');
    
    -- CreateTable
    CREATE TABLE "BaseCompany" (
        "id" UUID NOT NULL DEFAULT public.gen_random_uuid(),
        "name" TEXT NOT NULL,
        "address1" TEXT,
        "address2" TEXT,
        "city" TEXT,
        "state" TEXT,
        "zipCode" TEXT,
        "phone" TEXT,
        "website" TEXT,
        "country" TEXT,
        "isConfirmed" BOOLEAN DEFAULT false,
        "confirmedOn" TIMESTAMP(3),
        "confirmedById" UUID,
        "isActive" BOOLEAN NOT NULL DEFAULT true,
        "isDelete" BOOLEAN NOT NULL DEFAULT false,
        "createdById" UUID,
        "createdOn" TIMESTAMP(3),
        "modifiedById" UUID,
        "modifiedOn" TIMESTAMP(3),
        "deletedById" UUID,
        "deletedOn" TIMESTAMP(3),
    
        PRIMARY KEY ("id")
    );
    
  8. janpio commented on Apr 12, 2021

    @janpio
    Contributor

    At the top optimally, so it will be available when the CREATE TABLE is executed.

  9. erdoganabaci commented on Apr 12, 2021

    @erdoganabaci
    Author

    @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 commandnpx prisma migrate dev --create-only and npx prisma migrate dev

    Here there is screenshot

    Screen Shot 2021-04-12 at 21 39 15

  10. janpio commented on Apr 12, 2021

    @janpio
    Contributor

    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.

  11. tomhoule commented on Apr 12, 2021

    @tomhoule
    Contributor

    @erdoganabaci not sure, but I think the CREATE EXTENSION might need to be in a separate migration file that is executed first, before you can use it.

  12. erdoganabaci commented on Apr 12, 2021

    @erdoganabaci
    Author

    @tomhoule @janpio @pantharshit00 I tried all possibilities one of them is to create a new initial migration.sql include these commands

    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
    CREATE EXTENSION IF NOT EXISTS "pgcrypto";
    

    After it created with public.gen_random_uuid() then the second time i changed schema.prisma then it returns the same error.

    Error:
    Database error
    Error querying the database: db error: ERROR: function public.gen_random_uuid() does not exist
    

    After that, I deleted public function name in the prisma.schema gen_random_uuid() only this command applied.
    Then return the same error, unfortunately.

    Error: Database error
    Error querying the database: db error: ERROR: function gen_random_uuid() does not exist
    

    Screen Shot 2021-04-12 at 23 54 00

  13. tomhoule commented on Apr 13, 2021

    @tomhoule
    Contributor

    @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_migration folder to 0000_initial_migration?

  14. 32 remaining items

  15. added
    tech/engines
    on Mar 24, 2022
  16. kokokenada commented on Jun 15, 2022

    @kokokenada

    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 run prisma migrate dev. The only workaround I have found so far is to use a shadow database and manually add the extension there.

  17. skplunkerin commented on Mar 4, 2023

    @skplunkerin

    Just ran into this same issue, but in my case it was caused by npx prisma db pull not 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 init to 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 of ERROR: function public.gen_random_uuid() does not exist.

    I would expect either (or both) npx prisma db pull and npx prisma migrate dev --name init commands to be smart enough to pull down installed extensions and add those to the initialized migration files automatically.

  18. janpio commented on Mar 4, 2023

    @janpio
    Contributor

    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.)

  19. skplunkerin commented on Mar 6, 2023

    @skplunkerin

    @janpio thanks, I didn't know this was something I could enable. I'll give this a try 💪

  20. skplunkerin commented on Mar 7, 2023

    @skplunkerin

    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.)

    Testing this, it doesn't pull down any extensions. Do you know where I can share my feedback on this?

  21. janpio commented on Mar 10, 2023

    @janpio
    Contributor

    Open a new issue please and provide all the information it asks for. Thanks.

  22. victorsfranco commented on Mar 11, 2024

    @victorsfranco

    Full SQL for creating the postgres uuid extension:

    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

    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"

  23. kiwavi commented on Dec 26, 2024

    @kiwavi

    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 the migrations.sql file to include the SQL for that, then run npx prisma migrate dev again to apply the migration.

    Worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/2-confirmedkind/bugstatus/needs-actiontech/enginestopic: errortopic: postgresqlExtensions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions