self-relation
Greetings! Could someone help me? I haven't been able to understand this. I'm checking the followers and following of a user and everything appears correct in my db, but then when I check it the result is reversed. Any idea why? That 1 should appear in following and not in followers.
22 Replies
Do you mean the value in
followingId
and followerId
should be replaced?
Can you please elaborate?Currently there are 2 users, the idea is that user A follows user B
To represent this follow-up, there must be a table containing AId and BId, I named it Follow
As you can see, A or followerId already follows B or followingId
So when I check A and his followers it should return a 0, and his following should return a 1, but it comes out the other way around. And I'm very frustrated because I've already checked every possible error in detail and I don't see anything wrong T.T @Nurul (Prisma)
final result is inverted
I have deleted package.lock, updated dependencies, asked many colleagues, etc. and I have not been able to find the error, I have checked my schema a thousand times, I am dead, I have asked chatGPT. I have read the doc
I really don't want to leave Prisma, I like it a lot. And I don't see any alternatives either. So I don't know what to do.
Can you share your
schema.prisma
file? Also, how are you inserting the record in Follow
table?yes 1s
@Nurul (Prisma)
Let me create a minimal working example with your schema and share it with you
I saw you writing bro xD @Nurul (Prisma)
You should get the followers and following counts separately like this.
I used this schema:
And this is my script.ts file:
Now here is the correct output that you would expect
but _count works correctly for you?
My idea is to consult the user directly and from there select some data from their relationships, so as not to have to make 2 queries.
The reason your query shows inverted numbers is because this
would not return the number of followers and number of following as you are expecting.
It shows all the records in the
Follows
table that has followerId: 99f977...
and all followingId that has value 99f977...
which is not what you want.mmm ok ok
As you can see in the screenshot, you had 1 record with followeId: 99f and 0 records with followingId: 99f which is what was returned.