Home > Software design >  PostgreSQL error : the "PostAudienceEnum" type already exists
PostgreSQL error : the "PostAudienceEnum" type already exists

Time:07-25

I generated a migrate.sql file from the prisma ORM which I then imported into my PostgreSQL database from Query Tool. However when I run this file I get the error: the "PostAudienceEnum" type already exists . I don't know why yet I did declare PostAudienceEnum as an enum . Here are the lines where we find the enumeration PostAudienceEnum:

-- CreateEnum
CREATE TYPE "PostAudienceEnum" AS ENUM ('PUBLIC', 'FRIENDS', 'ONLY_ME', 'SPECIFIC');

CREATE TABLE "Post" (
       ...
    "audience" "PostAudienceEnum" NOT NULL DEFAULT E'PUBLIC',
       ...
    CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
);

This file was designed from my prisma schematic. I don't know how to modify it without messing up my database and why PostgreSQL throws this error.

CodePudding user response:

You might be getting this error if the database already has data, and you're attempting to manually execute the SQL file against the database. You should only use prisma migrate to manage changes in your schema against your database. Prisma internally records what migrations have and have not been executed against the database, and attempting to run an SQL file manually outside of prisma migrate defeats the purpose of using it in the first place.

Migrate docs: https://www.prisma.io/docs/concepts/components/prisma-migrate

You should ONLY use Prisma Migrate to make changes to your database tables/columns/relationships, and not an external tool. However, if you are developing your database FIRST and then looking to keep your Prisma schema up to date with your database (and not the other way around), you will want to introspect your database. Same deal applies: Prisma knows what parts of your database are reflected in your Prisma schema and what's not.

Introspection docs: https://www.prisma.io/docs/concepts/components/introspection

  • Related