Hi, I'm having issues using a many-to-many self-r...
# orm-help
s
Hi, I'm having issues using a many-to-many self-relation similar to the example used in the Prisma docs. I'm running into an error when creating a record along with its relation. The error I'm getting is this.
Copy code
PrismaClientValidationError: Unknown arg `follower` in data.followers.create.0.follower for type FollowsCreateWithoutFollowerInput. Did you mean `following`?
 Argument following for data.followers.create.0.following is missing.
I posted this question with more details on stackoverflow https://stackoverflow.com/questions/70520589/prisma-many-to-many-self-relation-error-due-to-relation-and-generated-type-inpu If anyone has any ideas I would greatly appreciate it. Thanks in advance.
m
did u try to use
connectOrCreate
?
s
I just tried and am getting the same error.
m
i am building the model and i am trying the same query , just a moment
👍 1
s
I think the gist of the issue is that the
followers
relationship on the
Follows
table generates a
FollowsCreateWithoutFollowerInput
and I don't believe that makes sense. It should create a
FollowsCreateWithoutFollowingInput
since when a
Person
has
followers
then they are the
following
(the one being followed) and we just don't know who the
follower
is, so the input should contain the value for the
follower
m
i am just curios why do u need to create person then add followers to the same person !
could you please try this !
Copy code
const person = await prisma.person.create({
    data: {
      followers: {
        create: {
          followingId: 1,
        },
      },
    },
  });
  return person;
could you please tell me what you are trying to do here
s
i am just curios why do u need to create person then add followers to the same person !
I want to add that person's followers, (the people that are following that person). If your question is why would I want that. I can switch the example around to something more intuitive. Where I create a person and add who they're
following
. I would still get the same error.
could you please try this !
That works, but I'm trying to add
followerId: 1
not
followingId
. When adding
followers
, the created person is the one being followed (followingId). So I don't need to input that id, I already know it.
could you please tell me what you are trying to do here
Create a person and add followers to them.
m
okay ... but what you are trying to do will violate the relationship if i am not mistaken !
s
That seems to be the case, but my question is why. It doesn't make sense. Shouldn't I be able to create a Person and add to them people they follow, by inputting the id of the Person they want to follow? The way prisma is set up it allows me to add people they want to follow by inputting the follower (which is the currently created person.) this doesn't make sense to me. I must be missing something fundamental.
m
Here is an example what you can do after creating a user !
Copy code
const person1 = await prisma.person.create({
    data: {
      name: 'Jone Doe1',
      following: {
        create: {
          followerId: 1,
        },
      },
    },
  });
OR
Copy code
const person2 = await prisma.person.create({
    data: {
      name: 'Jone Doe2',
      followers: {
        create: {
          followingId: 1,
        },
      },
    },
  });
  return { person1, person2 };
test
is following
Jone Doe2
Jone Doe1
is following test <--------------><--------------><--------------><--------->
test have one follower and following one person
<--------------><--------------><--------------><--------->
s
In your first example when creating
following
why would I input the
followerId
, the current person is the follower. I want to input the
followingId
. Is what I'm saying making sense?
In example 1.
followerId
should be the id of the follower (of
Jone Doe1
not of
test
). The follower is the one who is following the other person, so if
Jone Doe1
is following
test
like you say, then the
followerId
should be
2
not
1
. I think this may be a bug in Prisma, do you agree with what I'm saying?
y
@Shmuel After testing your example this seems to be a bug in Prisma.
@Shmuel Prisma seems to be mixing up the relations even when doing a GET. Calling
person.following
returns the followers and calling
person.followers
return the person's followees. I'd suggest you open an issue with Prisma.
s
Thanks @Yaakov and @Muhannad I added a bug report https://github.com/prisma/prisma/issues/10901