-
Notifications
You must be signed in to change notification settings - Fork 174
Open
Labels
Area: ParserbugSomething isn't workingSomething isn't workinggood first issueGood for newcomersGood for newcomers
Description
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
- Parse a Prisma schema with multiple tables that each have an
id
field as primary key - Generate DDL using the parser
- 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
- This follows PostgreSQL's default constraint naming conventions from
prisma migrate
- Related discussion: feat: add support for table-level primary key constraints #2258
Metadata
Metadata
Assignees
Labels
Area: ParserbugSomething isn't workingSomething isn't workinggood first issueGood for newcomersGood for newcomers