Django

Code

Changeset 9251

Show
Ignore:
Timestamp:
10/24/08 01:09:47 (3 months ago)
Author:
mtredinnick
Message:

Fixed #9406 -- Ensure that each database column is only represented once in the
"ORDER BY" clause of an SQL statement.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/sql/query.py

    r9206 r9251  
    622622        else: 
    623623            asc, desc = ORDER_DIR['DESC'] 
     624 
     625        # It's possible, due to model inheritance, that normal usage might try 
     626        # to include the same field more than once in the ordering. We track 
     627        # the table/column pairs we use and discard any after the first use. 
     628        processed_pairs = set() 
     629 
    624630        for field in ordering: 
    625631            if field == '?': 
     
    639645                col, order = get_order_dir(field, asc) 
    640646                table, col = col.split('.', 1) 
    641                 elt = '%s.%s' % (qn(table), col) 
    642                 if not distinct or elt in select_aliases: 
    643                     result.append('%s %s' % (elt, order)) 
     647                if (table, col) not in processed_pairs: 
     648                    elt = '%s.%s' % (qn(table), col) 
     649                    processed_pairs.add((table, col)) 
     650                    if not distinct or elt in select_aliases: 
     651                        result.append('%s %s' % (elt, order)) 
    644652            elif get_order_dir(field)[0] not in self.extra_select: 
    645653                # 'col' is of the form 'field' or 'field1__field2' or 
     
    647655                for table, col, order in self.find_ordering_name(field, 
    648656                        self.model._meta, default_order=asc): 
    649                     elt = '%s.%s' % (qn(table), qn2(col)) 
    650                     if distinct and elt not in select_aliases: 
    651                         ordering_aliases.append(elt) 
    652                     result.append('%s %s' % (elt, order)) 
     657                    if (table, col) not in processed_pairs: 
     658                        elt = '%s.%s' % (qn(table), qn2(col)) 
     659                        processed_pairs.add((table, col)) 
     660                        if distinct and elt not in select_aliases: 
     661                            ordering_aliases.append(elt) 
     662                        result.append('%s %s' % (elt, order)) 
    653663            else: 
    654664                col, order = get_order_dir(field, asc) 
  • django/trunk/tests/regressiontests/model_inheritance_regress/models.py

    r8932 r9251  
    258258>>> _ = QualityControl.objects.create(headline="Problems in Django", pub_date=datetime.datetime.now(), quality=10, assignee="adrian") 
    259259 
     260# Ordering should not include any database column more than once (this is most 
     261# likely to ocurr naturally with model inheritance, so we check it here). 
     262# Regression test for #9390. This necessarily pokes at the SQL string for the 
     263# query, since the duplicate problems are only apparent at that late stage. 
     264>>> sql = ArticleWithAuthor.objects.order_by('pub_date', 'pk').query.as_sql()[0] 
     265>>> fragment = sql[sql.find('ORDER BY'):] 
     266>>> pos = fragment.find('pub_date') 
     267>>> fragment.find('pub_date', pos + 1) == -1 
     268True 
     269 
    260270"""}