Skip to content

🐛 Prisma parser generates duplicate constraint names across tables #3075

@MH4GF

Description

@MH4GF

Problem Description

The Prisma parser generates DDL with duplicate constraint names when multiple tables have the same field names (e.g., id as primary key), causing PostgreSQL execution errors.

Steps to Reproduce

  1. Parse a Prisma schema with multiple tables that each have an id field as primary key
  2. Generate DDL using the parser
  3. Execute the generated DDL in PostgreSQL

Current Behavior

Generated DDL contains duplicate constraint names:

ALTER TABLE "media" ADD CONSTRAINT "PRIMARY_id" PRIMARY KEY ("id");
ALTER TABLE "users" ADD CONSTRAINT "PRIMARY_id" PRIMARY KEY ("id");

Error Message

ALTER TABLE "users" ADD CONSTRAINT "PRIMARY_id" PRIMARY KEY ("id"), Error: {"error":"relation \"PRIMARY_id\" already exists"};

Expected Behavior

Constraint names should match the actual naming conventions that PostgreSQL uses when migrations are applied. For example, when Prisma migrations are executed against PostgreSQL, the database creates constraints with these patterns:

  • Primary keys: {tablename}_pkey
  • Foreign keys: {tablename}_{column}_fkey
  • Unique constraints: {tablename}_{column}_key

Example of actual PostgreSQL constraint names:

CREATE TABLE "SampleMixed" (
    "id" TEXT NOT NULL,
    -- other columns...
    CONSTRAINT "SampleMixed_pkey" PRIMARY KEY ("id")
);

ALTER TABLE "Profile" ADD CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

CREATE UNIQUE INDEX "User_username_key" ON "User"("username");

Affected Code

  • File: frontend/packages/schema/src/parser/prisma/parser.ts
  • Function: processModelField
  • Location: if (field.isId) { block where constraint names are generated

Solution Direction

The constraint naming should be updated to match the actual values that PostgreSQL sets when migrations are applied, ensuring compatibility and preventing naming conflicts.

Related Information

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions