summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
AgeCommit message (Collapse)Author
2012-12-20Fixed #18854 -- Join promotion in disjunction casesAnssi Kääriäinen
The added promotion logic is based on promoting any joins used in only some of the childs of an OR clause unless the join existed before the OR clause addition.
2012-12-20Fixed #19500 -- Solved a regression in join reuseAnssi Kääriäinen
The ORM didn't reuse joins for direct foreign key traversals when using chained filters. For example: qs.filter(fk__somefield=1).filter(fk__somefield=2)) produced two joins. As a bonus, reverse onetoone filters can now reuse joins correctly The regression was caused by the join() method refactor in commit 68847135bc9acb2c51c2d36797d0a85395f0cd35 Thanks for Simon Charette for spotting some issues with the first draft of the patch.
2012-12-17Replaced '__' with LOOKUP_SEP in sql/query.pyAnssi Kääriäinen
Thanks to Simon Charette for report.
2012-12-16Fixed #10790 -- Refactored sql.Query.setup_joins()Anssi Kääriäinen
This is a rather large refactoring. The "lookup traversal" code was splitted out from the setup_joins. There is now names_to_path() method which does the lookup traveling, the actual work of setup_joins() is calling names_to_path() and then adding the joins found into the query. As a side effect it was possible to remove the "process_extra" functionality used by genric relations. This never worked for left joins. Now the extra restriction is appended directly to the join condition instead of the where clause. To generate the extra condition we need to have the join field available in the compiler. This has the side-effect that we need more ugly code in Query.__getstate__ and __setstate__ as Field objects aren't pickleable. The join trimming code got a big change - now we trim all direct joins and never trim reverse joins. This also fixes the problem in #10790 which was join trimming in null filter cases.
2012-12-16Fixed #18816 -- Removed "trim" argument from add_filter()Anssi Kääriäinen
The trim argument was used by split_exclude() only to trim the last join from the given lookup. It is cleaner to just trim the last part from the lookup in split_exclude() directly so that there is no need to burden add_filter() with the logic needed for only split_exclude().
2012-11-28Fixed #14694 -- Made ``defer()`` work with reverse relationsTai Lee
Reverse o2o fields are now usable with defer.
2012-11-24Updated stale docstring of setup_joinsAnssi Kääriäinen
2012-11-23Fixed #18375 -- Removed dict-ordering dependency for F-expressionsAnssi Kääriäinen
F() expressions reuse joins like any lookup in a .filter() call - reuse multijoins generated in the same .filter() call else generate new joins. Also, lookups can now reuse joins generated by F(). This change is backwards incompatible, but it is required to prevent dict randomization from generating different queries depending on .filter() kwarg ordering. The new way is also more consistent in how joins are reused.
2012-11-13Removed use of SortedDict for query.alias_refcountAnssi Kääriäinen
This will have a smallish impact on performance. Refs #19276.
2012-10-31Removed dupe_avoidance from sql/query and sql/compiler.pyAnssi Kääriäinen
The dupe avoidance logic was removed as it doesn't seem to do anything, it is complicated, and it has nearly zero documentation. The removal of dupe_avoidance allowed for refactoring of both the implementation and signature of Query.join(). This refactoring cascades again to some other parts. The most significant of them is the changes in qs.combine(), and compiler.select_related_descent().
2012-10-27Fixed #19190 -- Refactored Query select clause attributesAnssi Kääriäinen
The Query.select and Query.select_fields were collapsed into one list because the attributes had to be always in sync. Now that they are in one attribute it is impossible to edit them out of sync. Similar collapse was done for Query.related_select_cols and Query.related_select_fields.
2012-10-25Fixed regression caused by #19102Anssi Kääriäinen
2012-10-25Fixed #19102 -- Fixed fast-path delete for modified SELECT clause casesAnssi Kääriäinen
There was a bug introduced in #18676 which caused fast-path deletes implemented as "DELETE WHERE pk IN <subquery>" to fail if the SELECT clause contained additional stuff (for example extra() and annotate()). Thanks to Trac alias pressureman for spotting this regression.
2012-10-08Fixed #19087 -- Ensured query's base table is never LOUTER joinedAnssi Kääriäinen
This fixes a regression created by join promotion logic refactoring: 01b9c3d5193fe61b82ae8b26242a13fdec22f211 Thanks to Ivan Virabyan for the report.
2012-09-08Internal refactoring; moving LOOKUP_SEP up one level.Malcolm Tredinnick
In an ideal world, nothing except django.db.models.query should have to import stuff from django.models.sql.*. A few things were needing to get hold of sql.constants.LOOKUP_SEP, so this commit moves it up to django.db.models.constants.LOOKUP_SEP. There are still a couple of places (admin) poking into sql.* to get QUERY_TERMS, which is unfortunate, but a slightly different issue and harder to adjust.
2012-09-07Cleaned up some small bits of the ORM, including removing an import *.Alex Gaynor
2012-08-25Fixed #16715 -- Fixed join promotion logic for nested nullable FKsAnssi Kääriäinen
The joins for nested nullable foreign keys were often created as INNER when they should have been OUTER joins. The reason was that only the first join in the chain was promoted correctly. There were also issues with select_related etc. The basic structure for this problem was: A -[nullable]-> B -[nonnull]-> C And the basic problem was that the A->B join was correctly LOUTER, the B->C join not. The major change taken in this patch is that now if we promote a join A->B, we will automatically promote joins B->X for all X in the query. Also, we now make sure there aren't ever join chains like: a LOUTER b INNER c If the a -> b needs to be LOUTER, then the INNER at the end of the chain will cancel the LOUTER join and we have a broken query. Sebastian reported this problem and did also major portions of the patch.
2012-08-21Fixed #17886 -- Fixed join promotion in ORed nullable queriesAnssi Kääriäinen
The ORM generated a query with INNER JOIN instead of LEFT OUTER JOIN in a somewhat complicated case. The main issue was that there was a chain of nullable FK -> non-nullble FK, and the join promotion logic didn't see the need to promote the non-nullable FK even if the previous nullable FK was already promoted to LOUTER JOIN. This resulted in a query like a LOUTER b INNER c, which incorrectly prunes results.
2012-08-12Fixed #18731 -- Cleaned up split_exclude's use of can_reuseAnssi Kääriäinen
The outer query's set of reusable joins (can_reuse) was passed to the inner query's add_filter call. This was incorrect.
2012-08-08remove a bunch of unnescesarry iterkeys() callsAlex Gaynor
2012-08-07[py3] Ported django.utils.encoding.Aymeric Augustin
* Renamed smart_unicode to smart_text (but kept the old name under Python 2 for backwards compatibility). * Renamed smart_str to smart_bytes. * Re-introduced smart_str as an alias for smart_text under Python 3 and smart_bytes under Python 2 (which is backwards compatible). Thus smart_str always returns a str objects. * Used the new smart_str in a few places where both Python 2 and 3 want a str.
2012-08-07[py3] Fixed access to dict keys/values/items.Aymeric Augustin
2012-07-17Switched to use a more idiomatic construct.Alex Gaynor
2012-07-17Fixed #17497 -- Corrected FieldError message in add_fields()Anssi Kääriäinen
The erroneous message was user visible in values_list() calls. Thanks to ojii for report and review, and to antoviaque for the patch.
2012-06-26Fixed #17485 -- Made defer work with select_relatedAnssi Kääriäinen
This commit tackles a couple of issues. First, in certain cases there were some mixups if field.attname or field.name should be deferred. Field.attname is now always used. Another issue tackled is a case where field is both deferred by .only(), and selected by select_related. This case is now an error. A lot of thanks to koniiiik (Michal Petrucha) for the patch, and to Andrei Antoukh for review.
2012-05-10Replaced foo.next() by next(foo).Claude Paroz
This new syntax for next() has been introduced in Python 2.6 and is compatible with Python 3.
2012-05-09Fix proxy model Query.remove_inherited_models()Anssi Kääriäinen
Fixed #18248 -- proxy models were added to included_inherited_models in sql.query.Query. The variable is meant to be used for multitable inheritance only. This mistake caused problems in situations where proxy model's query was reused.
2012-04-29Fixed #17644 -- Changed Query.alias_map to use namedtuplesAdrian Holovaty
This makes the code easier to understand and may even have a benefit in memory usage (namedtuples instead of dicts). Thanks, lrekucki and akaariai
2012-04-29Fixed #18013 -- Use the new 'as' syntax for exceptions.Claude Paroz
Thanks Clueless for the initial patch. Note that unittest has been purposely left out (external package only used by Python 2.6).
2012-04-29Removed unused variable from sql/query.pyAnssi Kääriäinen
2012-04-29Prevent Oracle from changing field.null to TrueAnssi Kääriäinen
Fixed #17957 -- when using Oracle and character fields, the fields were set null = True to ease the handling of empty strings. This caused problems when using multiple databases from different vendors, or when the character field happened to be also a primary key. The handling was changed so that NOT NULL is not emitted on Oracle even if field.null = False, and field.null is not touched otherwise. Thanks to bhuztez for the report, ramiro for triaging & comments, ikelly for the patch and alex for reviewing.
2012-04-29Simplified QuerySet field.null handlingAnssi Kääriäinen
QuerySet had previously some complex logic for dealing with nullable fields in negated add_filter() calls. It seems the logic is leftover from a time where the WhereNode wasn't as intelligent in handling field__in=[] conditions. Thanks to aaugustin for comments on the patch.
2012-04-08Fixed #18014 -- Removed rev_join_map from sql/query.py.Anssi Kääriäinen
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17878 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2012-03-22Used SortedDict instead of dict to avoid random errors that may occur when ↵Aymeric Augustin
dict randomization is enabled in Python. Refs #17758. Thanks Łukasz Rekucki. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17777 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2012-02-22Fixed #17678 -- Corrected setup of _meta.proxy_for_model and added ↵Carl Meyer
_meta.concrete_model. Thanks Anssi Kääriäinen. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17573 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2012-02-05Fixed #11670 -- Prevented genuine model fields named 'year', 'month', 'gt', ↵Julien Phalip
'lt' etc. from being mistaken for lookup types in lookups across relations. Thanks to andy for the report, to jpwatts for the initial patch and to Anssi Kääriäinen and Alex Gaynor for the reviews. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17450 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22Added support for modifying the effect of ``DISTINCT`` clauses so theyRamiro Morales
only consider some fields (PostgreSQL only). For this, the ``distinct()`` QuerySet method now accepts an optional list of model fields names and generates ``DISTINCT ON`` clauses on these cases. Thanks Jeffrey Gelens and Anssi Kääriäinen for their work. Fixes #6422. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17244 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-17Fixed various dodgy behavioursAdrian Holovaty
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17226 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-11-13Fixed #13640: Avoid generating an exception when a model has an attribute ↵Karen Tracey
named 'evaluate'. Thanks LukaszKorzybski and tobias. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17093 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-09-10Remove a bunch of deadcode/dead imports.Alex Gaynor
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16794 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-09-09Fixed #7596. Added Model.objects.bulk_create, and make use of it in several ↵Alex Gaynor
places. This provides a performance benefit when inserting multiple objects. THanks to Russ for the review, and Simon Meers for the MySQl implementation. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16739 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-08-23Allow "pk" as a field alias in QuerySet.only() calls.Malcolm Tredinnick
Thanks to GDorn for the patch. Fixed #15494. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16668 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-08-23Fixed an isnull=False filtering edge-case. Fixes #15316.Malcolm Tredinnick
The bulk of this patch is due to some fine analysis from Aleksandra Sendecka. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16656 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-08-23Added convenience method for viewing Query SQL without params.Malcolm Tredinnick
This is the old Query.as_sql() method revived: it's like Query.__str__, but the parameters aren't substituted into the placeholders. Thus, it's a more accurate representation of the SQL the (default) backend will see. Entirely internal. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16655 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-08-22Fixed #14876 -- Ensure that join promotion works correctly when there are ↵Russell Keith-Magee
nullable related fields. Thanks to simonpercivall for the report, oinopion and Aleksandra Sendecka for the original patch, and to Malcolm for helping me wrestle the edge cases to the ground. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16648 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-06-28Fixed #5535 -- Allow using an explicit foreign key in get() calls. Thanks, ↵Jannis Leidel
Michal Petrucha. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16473 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-05-21Fixed #14476 -- Fixed resolution of automatically generated annotation names ↵Ramiro Morales
so e.g. filtering based on them works. Thanks dirleyls for the report and patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16252 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-05-15Fixed #15790 -- Fixed QuerySet only() and defer() methods behavior with ↵Ramiro Morales
proxy models. Thanks Michal Modzelewzki for the report and patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16228 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-05-05Fixed #15823 - incorrect join condition when combining Q objectsLuke Plant
Thanks to dcwatson for the excellent report and patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16159 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-04-22Fixed #14729 -- RawQuerySet.__repr__ fails when params passed as list. ↵Jannis Leidel
Thanks, intgr for ticket and accuser for patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16088 bcc190cf-cafb-0310-a4f2-bffc1f526a37