Using Prisma 3.7.0
.
I'm following this example in the docs
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])
}
Then I try creating a User
along with the people they're following
, with the below code.
const person = await prisma.person.create({
data: {
following: {
create: [
{ following: { connect: { id: 1 } } }
]
}
}
});
I'm getting the following (see what I did there :)) 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'
}
I believe the reason I'm getting this error is because the following[]
relation creates an input as mentioned in the error message FollowsCreateWithoutFollowingInput
, meaning it's expecting a follower
relation and not a following
, as the following
will be the Person
I'm currently creating, and I just need to tell it who is the follower
Person
.
However this doesn't make sense to me. When I'm creating a Person
along with its people they're following
, I understand that to be an array of Persons
who the person I'm currently creating is following
. If so, then a Follows
record in the following
array contains the current Person
(the one I'm creating) as the follower
relation and some other Person
as the following
relation. And the relation I should be inputting is the following
and not the follower
. Therefore the input type that prisma should generate should be FollowsCreateWithoutFollowerInput
instead of FollowsCreateWithoutFollowingInput
.
What am I missing, in my understanding?
I looked at the below resources during my research on this.
- count self relation on Prisma error: table name specified more than once. This is discussing a different issue, using the same example.
- https://github.com/prisma/prisma/discussions/3960. This discusses how to create the same type of relation, where the join table references the same table for both ids. But it doesn't explain how to create records once the relationships are defined.
- One-to-many self-relation in prisma schema. This shows how to create a record, but its not using the join tables relation during create it's using a property. Also it's not exactly the same case as the join table seems to be the same table.