First of all, thank you for the well thought-out response, I appreciate your efforts. :)
Here is the crux of the matter, without any examples, and you phrased it best: > "You've overridden that by > specifying a to_field whose value is not unique (allowing that may be > a bug in Django)." The whole point is that I think the behavior is a bug in Django, because it's inconsistent as to which the count() is generating. I was hoping someone knew the core well enough to explain this inconsistency. Core devs, is this a bug, or do all of us who have commented on it not understand why this behavior gives these apparently inconsistent values? I'm going to have to generate a different example, because what I'm using here is obviously causing people to think about the wrong part of the problem. The problem is that .select_related().count() has wildly differing behavior depending on whether there's filter() criteria on the ForeignKey table, and/or the use of distinct() prior to the count(). Our internal example that has a problem is related to the use of a OneToOneField, so there is no many-to-many relationship there. In fact, it's a zero-to-one relation or a one-to-one relation, but it exhibits the same behavior because OneToOneFields function like ForeignKeys. People sometimes get upset and confused by OneToOneField, but it looks like I just made it worse for everybody. :) So, my example (which attempted to simplify things with ForeignKeys) just caused people to try to think about the relationships here, and that's not what was really the problem. For that, I apologize. Thanks, George Karen Tracey wrote: > On 10/31/07, *George Vilches* <[EMAIL PROTECTED] > <mailto:[EMAIL PROTECTED]>> wrote: > > [snip] > > Here is what I want to count: > > "All the quantity of items necessary to complete all the selected > assemblies". > > That's not thinking in SQL, that's very much thinking about the objects > at hand. I have Assemblies, and I need to know the total number of > Items. That's a concept that can be thought about strictly from the > objects. > > So, if I have two assemblies, and Assembly 1 connects to 2 items, and > Assembly 2 connects to 3 items, I want the total result of 5 when I > do a > count() where I refer to the Items (select_related()). > > > A ForeignKey in the Assembly model (as you have it now) is not the right > way to describe the relationship you state in the above sentence. A > ForeignKey is used to describe a many-to-one relationship, where the > "many" side is the model containing the ForeignKey and the "one" side is > the model that is the target of the ForeignKey. That is, many Assembly > instances might "point to" the same Item instance, but each individual > Assembly is associated with exactly one Item. So a single Item would > have a set of Assembly objects "connected" to it, not vice-versa. > > This happens naturally by default because the ForeignKey uses the > primary key field of the target model. You've overridden that by > specifying a to_field whose value is not unique (allowing that may be a > bug in Django). I think this is the crux of what's causing the > differences you see in the various ways of determining count() -- Django > is assuming a unique relationship which in you case does not exist. > > Now, as to fixing it for your case, I'm not sure how to do it because > I'm not clear on the relationship between Assembly and Item. Is it > many-to-one (only reversed from the way currently defined in your > models) or many-to-many? If it really is many-to-one, then moving the > ForeignKey field over into the Item model may be all you need to do > (plus turning around your thinking, which often trips me up when trying > to work out ForeignKey relationships). > > Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---