question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple @relations between models throw errors

See original GitHub issue

Bug description

When you try to link 2 or more relations in UserPermission model to the User model its not working and error pop up.

 ERROR  Oops, an unexpected error occured!
error: Error validating model "User": Automatic related field generation would cause a naming conflict. Please add an explicit opposite relation field.
  -->  schema.prisma:28
   | 
27 |     updated                 User[]              @relation("ClientUpdatedByUser")
28 |     updatedUserPermission   User[]              @relation("UserPermissionUpdatedByUser")
   | 
error: Error validating model "UserPermission": Automatic related field generation would cause a naming conflict. Please add an explicit opposite relation field.
  -->  schema.prisma:41
   | 
40 |     updatedAt       DateTime        @updatedAt
41 |     createdBy       User?           @relation("UserPermissionCreatedByUser")
   | 
error: Error validating model "UserPermission": Automatic related field generation would cause a naming conflict. Please add an explicit opposite relation field.
  -->  schema.prisma:42
   | 
41 |     createdBy       User?           @relation("UserPermissionCreatedByUser")

How to reproduce

  1. Setup a clean prisma2@2.0.0-preview024 standard env
  2. Run prisma2 migrate save --experimental

Expected behavior

Multiple 2 or more (3, 4, 5,…) relations between 2 models should work, because as shown in the example sometimes its needed. Otherwise the createdBy / updatedBy columns getting Int or String data to save, and this is not prisma2 like.

Prisma information

Failing:

datasource mysql {
    provider = "mysql"
    url      = "mysql://user:password@localhost:3307/prisma"
}

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

model User {
    id                      Int                 @id @default(autoincrement())
    email                   String              @unique
    password                String
    firstname               String
    lastname                String
    userPermissions         UserPermission[]    @relation("UserPermissionLink")
    isBlocked               Boolean             @default(false)
    blockedReason           String?
    blockedAt               DateTime?
    createdAt               DateTime            @default(now())
    updatedAt               DateTime            @updatedAt
    createdBy               User?               @relation("ClientCreatedByUser")
    updatedBy               User?               @relation("ClientUpdatedByUser")
    created                 User[]              @relation("ClientCreatedByUser")
    createdUserPermission   User[]              @relation("UserPermissionCreatedByUser")
    updated                 User[]              @relation("ClientUpdatedByUser")
    updatedUserPermission   User[]              @relation("UserPermissionUpdatedByUser")
}

model UserPermission {
    id              Int             @id @default(autoincrement())
    client          Client
    user            User[]          @relation("UserPermissionLink")
    isBlocked       Boolean
    createdAt       DateTime        @default(now())
    updatedAt       DateTime        @updatedAt
    createdBy       User?           @relation("UserPermissionCreatedByUser")
    updatedBy       User?           @relation("UserPermissionUpdatedByUser")
}

Working, but without the second and third relation in UserPermission model createdBy / updatedBy User?

datasource mysql {
    provider = "mysql"
    url      = "mysql://user:password@localhost:3307/prisma"
}

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

model User {
    id                      Int                 @id @default(autoincrement())
    email                   String              @unique
    password                String
    firstname               String
    lastname                String
    userPermissions         UserPermission[]    
    isBlocked               Boolean             @default(false)
    blockedReason           String?
    blockedAt               DateTime?
    createdAt               DateTime            @default(now())
    updatedAt               DateTime            @updatedAt
    createdBy               User?               @relation("ClientCreatedByUser")
    updatedBy               User?               @relation("ClientUpdatedByUser")
    created                 User[]              @relation("ClientCreatedByUser")
    updated                 User[]              @relation("ClientUpdatedByUser")
}

model UserPermission {
    id              Int             @id @default(autoincrement())
    client          Client
    user            User[]          
    isBlocked       Boolean
    createdAt       DateTime        @default(now())
    updatedAt       DateTime        @updatedAt
    createdBy       Int?           
    updatedBy       Int?          
}

Environment & setup

On my local Macbook Pro, macOS 10.15.3

  • OS: Mac OS
  • Database: MySQL
  • Prisma version: prisma2@2.0.0-preview024, binary version: 377df4fe30aa992f13f1ba152cf83d5770bdbc85
  • Node.js version: v12.16.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
janpiocommented, Mar 26, 2020

I can also confirm this in the context of the Prisma VSCode extension: image

Or just prisma2 validate on the model you posted: image (This screenshot was made with current @prisma/cli@2.0.0-alpha.973)

So this is not migrate related.

0reactions
pantharshit00commented, Aug 5, 2020

Oh ok, then I am going to close it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ef core one to many relationship throw exception Cannot add ...
My Shop and Product entities have a one to many relationship, please see my models public ...
Read more >
Can't determine relationship between two or more fields
The relationships are both one to many. The table visual can't deal with that. Change it to a matrix and it will stop...
Read more >
Relations | Objection.js
Basically there are three ways to create a relationship between two tables A ... these situations and mention the words require loop in...
Read more >
Define Relationships Between SQLAlchemy Data Models
SQLAlchemy's ORM easily defines data models with relationships such as one-to-one, one-to-many, and many-to-many relationships.
Read more >
throw - JavaScript - MDN Web Docs - Mozilla
The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found