Skip to content

Error when generating from schema with polymorphic models - given constraint name ... has to be unique in the following namespace... #1058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andrewkucz opened this issue Feb 26, 2024 · 3 comments

Comments

@andrewkucz
Copy link

Description and expected behavior

I've been playing around with the 2.0 polymorphic features of zenstack and ran into an issue I can't seem to get around / find documented here:

"Generating Prisma schema" completes successfully but I run into the following error during "Generating PrismaClient enhancer"

error: Error parsing attribute "@relation": The given constraint name `UserFavorite_id_fkey` has to be unique in the following namespace: on model `UserFavorite` for primary key, indexes, unique constraints and foreign keys. Please provide a different name using the `map` argument.
  -->  schema.prisma:69
   | 
68 |     delegate_aux_personId String
69 |     delegate_aux_studio Studio @relation(fields: [id], references: [id])
70 |     delegate_aux_studioId String
   | 

Validation Error Count: 4
[Context: getDmmf]

Prisma CLI Version : 5.10.2
✖ Generating PrismaClient enhancer
Prisma: Failed to run "prisma generate

Here is a simplified version of my schema.zmodel:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
    id            String    @id @default(cuid())
    name          String

    userRankings UserRanking[]
    userFavorites UserFavorite[]
}


model Entity {
  id      String          @id @default(cuid()) 
  name    String  
  type String
  userRankings UserRanking[]
  userFavorites UserFavorite[]

  @@delegate(type)
}

model Person extends Entity {
}

model Studio extends Entity {
}


model UserRanking {
  id      String       @id @default(cuid()) 
  rank     Int

  entityId      String
  entity        Entity        @relation(fields: [entityId], references: [id], onUpdate: NoAction)
  userId   String
  user     User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: NoAction)
}

model UserFavorite {
  id      String       @id @default(cuid()) 

  entityId      String
  entity        Entity        @relation(fields: [entityId], references: [id], onUpdate: NoAction)
  userId   String
  user     User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: NoAction)
}

Based on the error message, I think a possible solution would be to add a map property to the relation attribute as it suggests. When I go in and modify node_modules\.zenstack\delegate.prisma just to try it out, the IDE error goes away at least so I think its promising. Maybe on generation this should be added? Or an option to specify this somewhere in the zmodel?

// delegate.prisma
-- delegate_aux_studio Studio @relation(fields: [id], references: [id])
++ delegate_aux_studio Studio @relation(fields: [id], references: [id], map: "studio")

Environment (please complete the following information):

  • ZenStack version: 2.0.0-alpha.1
  • Prisma version: 5.10.2
  • Database type: postgresql

Additional context

Oddly enough changing the generator provider to sqlite seems to remedy the issue (something prisma related?) but I was hoping to use postgresql

Thanks
-Andrew

@ymc9
Copy link
Member

ymc9 commented Feb 26, 2024

Hi @andrewkucz , thanks for reporting the issue. Yes, you're right, the generated delegate.prisma should include the "map" argument to disambiguate fk constraints. I'll make a fix in the next alpha release.

@andrewkucz
Copy link
Author

Hi @andrewkucz , thanks for reporting the issue. Yes, you're right, the generated delegate.prisma should include the "map" argument to disambiguate fk constraints. I'll make a fix in the next alpha release.

Thanks for the swift feedback @ymc9 -- thats great to hear! And props on an awesome library, excited to use it!

@ymc9
Copy link
Member

ymc9 commented Mar 9, 2024

Fixed in 2.0.0-alpha.6

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

No branches or pull requests

2 participants