Many to Many self-relation seems to generate the wrong `*CreateWithout*Input`
See original GitHub issueBug description
I think the gist of the issue is that the following
relationship on the Follows
table generates a FollowsCreateWithoutFollowingInput
and I don’t believe that makes sense. It should create a FollowsCreateWithoutFollowerInput
since when a Person
has people they’re following
then they are the follower
and we just don’t know who the Person
that they’re following
is, so the input should contain the value for the Person
they’re following
How to reproduce
- Add these two models to schema.prisma
model Person {
id Int @id @default(autoincrement())
name String?
followers Follows[] @relation("follower")
following Follows[] @relation("following")
}
model Follows {
follower Person @relation("follower", fields: [followerId], references: [id])
followerId Int
following Person @relation("following", fields: [followingId], references: [id])
followingId Int
@@id([followerId, followingId])
}
- Run the migration
npx prisma migrate dev
- Manually add a
Person
to the db to use as thePerson
you will befollowing
- Run the below code to get the error
const person = await prisma.person.create({
data: {
following: {
create: [
{ following: { connect: { id: 1 } } }
]
}
}
});
You should see the error.
/usr/src/app/node_modules/@prisma/client/runtime/index.js:34755
const error2 = new PrismaClientValidationError(renderErrorStr(validationCallsite));
^
PrismaClientValidationError: Unknown arg `following` in data.following.create.0.following for type FollowsCreateWithoutFollowingInput. Did you mean `follower`?
Argument follower for data.following.create.0.follower is missing.
at Object.validate (/usr/src/app/node_modules/@prisma/client/runtime/index.js:34755:20)
at PrismaClient._executeRequest (/usr/src/app/node_modules/@prisma/client/runtime/index.js:39749:17)
at consumer (/usr/src/app/node_modules/@prisma/client/runtime/index.js:39690:23)
at /usr/src/app/node_modules/@prisma/client/runtime/index.js:39694:49
at AsyncResource.runInAsyncScope (node:async_hooks:199:9)
at PrismaClient._request (/usr/src/app/node_modules/@prisma/client/runtime/index.js:39694:27)
at request (/usr/src/app/node_modules/@prisma/client/runtime/index.js:39799:77)
at _callback (/usr/src/app/node_modules/@prisma/client/runtime/index.js:40007:14)
at PrismaPromise.then (/usr/src/app/node_modules/@prisma/client/runtime/index.js:40014:23) {
clientVersion: '3.7.0'
}
Expected behavior
Since I’m trying to create a Person
with people they’re following
, I expect to input (connect
) a Person
that they’re following
not who the follower
is. The Person
I’m trying to create is the follower
of this following[]
relationship.
When prisma generates types from the schema, for the relationship following[]
it should create FollowsCreateWithoutFollowerInput
instead of FollowsCreateWithoutFollowingInput
.
More details about this issue can be found here:
- https://stackoverflow.com/questions/70520589/prisma-many-to-many-self-relation-error-due-to-relation-and-generated-type-inpu
- https://prisma.slack.com/archives/CA491RJH0/p1640787469302900
Prisma information
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "linux-musl"]
}
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}
model Person {
id Int @id @default(autoincrement())
name String?
followers Follows[] @relation("follower")
following Follows[] @relation("following")
}
model Follows {
follower Person @relation("follower", fields: [followerId], references: [id])
followerId Int
following Person @relation("following", fields: [followingId], references: [id])
followingId Int
@@id([followerId, followingId])
}
const person = await prisma.person.create({
data: {
following: {
create: [
{ following: { connect: { id: 1 } } }
]
}
}
});
Environment & setup
- OS:
Docker node:16-alpine
- Database:
SQLServer
- Node.js version:
v16.13.1
Prisma Version
prisma : 3.7.0
@prisma/client : 3.7.0
Current platform : linux-musl
Query Engine (Node-API) : libquery-engine 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/libquery_engine-linux-musl.so.node)
Migration Engine : migration-engine-cli 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/migration-engine-linux-musl)
Introspection Engine : introspection-core 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/introspection-engine-linux-musl)
Format Binary : prisma-fmt 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/prisma-fmt-linux-musl)
Default Engines Hash : 8746e055198f517658c08a0c426c7eec87f5a85f
Studio : 0.445.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:10 (3 by maintainers)
Top GitHub Comments
See the explicit many-to-many example here.
@p-kuen Reproduction welcome as well of course 👍