len(Friend.objects.friends(...)) is not correct
Closed this issue · 5 comments
I receive incorrect results for len(Friend.objects.friends(...))
for specific users. For example, users with 5 friends I receive len=6, users with 6 friends I receive len=7 etc. (The mistake is always +1). With version 1.9.1, I didn't check other versions. Sometimes I do receive the correct number of friends.
Hi @uri-rodberg
What's the extra item? Are any of the users duplicated in the queryset? I'm thinking maybe it's just a missing .distinct()
If you could write a PR with a failing test case, that'd go a long way helping us :)
I tried but .distinct()
doesn't solve the problem. I'm not sure if I know how to create a test which will fail. I'm just checking our server, and a person with 6 friends returns a result of 7 friends even with .distinct()
.
It might be a problem of using cache
, but I'm not sure.
Do you want me to check another query, maybe with the id of the 7 friends? Will it help?
Do you want me to check another query, maybe with the id of the 7 friends? Will it help?
Yes, I want to figure out what that extra item is. Could you also check if that the user you're querying friends for is not included?
Yes, I want to figure out what that extra item is. Could you also check if that the user you're querying friends for is not included?
What is the query you want me to use? I checked the models and I think this is the code:
def friends(self, user):
""" Return a list of all friends """
key = cache_key("friends", user.pk)
friends = cache.get(key)
if friends is None:
qs = (
Friend.objects.select_related("from_user", "to_user")
.filter(to_user=user)
.all()
)
friends = [u.from_user for u in qs]
cache.set(key, friends)
return friends
How do you want me to query the database?
I checked the IDs of the users (from Friend.objects.friends(...)
), and I found out that the user has 7 friends, but one of them is deactivated so I can't see him on the list of friends on the website. So I think it's not a problem of django-friendship. I'm sorry about the mistake. I'm closing this issue now.