Multiple @relations between models throw errors
See original GitHub issueBug 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
- Setup a clean prisma2@2.0.0-preview024 standard env
- 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:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I can also confirm this in the context of the Prisma VSCode extension:
Or just
(This screenshot was made with current
prisma2 validate
on the model you posted:@prisma/cli@2.0.0-alpha.973
)So this is not
migrate
related.Oh ok, then I am going to close it.