Duplicate rows when checking ID
|Reported by:||jnadro52||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.1|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I hope that this is a legitimate bug and I'm not wasting your time with something that is a slight misunderstanding of how the Django database layer works. Here we go...
I have two tables... a table that holds football matchups (texans vs. giants, for example) and a table that caches the statistics for the game. The table that caches the statistics would have a row per team, so this is what the data would look like:
ID team opponent matchup_id 1 texans giants 1 2 giants texans 1
So now I want to retrieve these two rows by matchup ID. Lets say the model names are "matchup" and "cache". So, I attempted to go retrieve them like so:
caches = cache.objects.filter(matchup=1)
When I do so, I do get two objects returned, but they are both the same cache row that is returned. So I would get a QuerySet containing two objects, both of which would have the same primary key.
What really confused me is that if I added a .distinct() method call to the end of that call, I would get the expected results. If it is the case where there is a join occurring I would understand getting duplicates, even though I would think there should be no join in this call, since i am just checking a foreign key. The kicker is that I always get row id 2 and never row id 1 when I make this call without the distinct call as well. If this were a cross-join, I should get multiple duplicates of all rows that meet the criteria, but I do not.
I hope I explained this well enough, please feel free to contact me at my given email if you have any questions.