Hi all,
In working on the patch that was committed to magic-removal as r1792, I
noticed that there are some areas of test non-coverage in the
many-to-many processing section of parse_lookup. However, writing the
test cases is proving a little hairy, because I can't work out what the
correct behaviour should be. Some community feedback would be
appreciated.
Given the following schema (the one used in the m2m test cases):
class Publication(models.Model):
title = models.CharField(maxlength=30)
class Article(models.Model):
headline = models.CharField(maxlength=100)
publications = models.ManyToManyField(Publication)
Consider the following kwarg queries:
1) Article.objects.get_list(publications__pk=1)
2) Article.objects.get_list(publications__title__startswith="Science")
3) Publication.objects.get_list(articles__headline__startswith="NASA")
As far as I can make out, this behaviour is an undocumented feature (or
rather, it is documented, but only in the context of many-to-one joins
where its interpretation is unambiguous). It isn't exercised by any
unit tests either, so a regression bug slipped in with r1792.
I could just write unit tests that represent r1791 behaviour, but the
existing behaviour doesn't sit quite right with me. Using r1791:
(1) is effectively interpreted as "include all articles that are in
publication 1"
(2) returns duplicate entries if the join matches multiple columns
(e.g., if an article appears in "Science Daily" and "Science Weekly",
it will appear twice in the results of (2), unless DISTINCT is
enabled).
(3) doesn't work, because a many-to-many field for Article is not on
the field list for Publication. This is the same thing preventing the
same style query over one-to-many relations.
These are not surprising as SQL results, but it doesn't really sit well
with me as an ORM query answer (and "don't be SQL'y, be Object'y" seems
to be a recurring theme in DB query discussions).
With that in mind, what is the intended interpretation of
publications__title__*? Is it "If a matching publication exists", or
"Duplicate for all matching publications" (or something else entirely)?
If it is the former, shouldn't the query arg reflect that fact? (which
then opens a whole nest of vipers regarding aggregate queries).
If it is the latter, how is this not SQL in sheeps clothing? Should
many-to-many queries like this be allowed at all?
If they should be allowed, should reverse m2m relation queries (and
therefore one-to-many relations too) be included in the action?
And, for bonus points, does any of this discussion belong as part of
the descriptor/field discussion?
Comments?
Yours,
Russ Magee