1. Thomas Hoffbauer
  2. django-mssql

Commits

Michael Manfre  committed 2a5d6af

#69: Fixed query column aliasing to account for different casing for same name. Thanks Tobias

  • Participants
  • Parent commits 610b314
  • Branches default

Comments (0)

Files changed (3)

File source/sqlserver_ado/query.py

View file
  • Ignore whitespace
             # Lop off ORDER... and the initial "SELECT"
             inner_select = _remove_order_limit_offset(raw_sql)
             outer_fields, inner_select = self._alias_columns(inner_select)
-            
-            
+
             # map a copy of outer_fields for injected subselect
             f = []
             for x in outer_fields.split(','):
             select_list, from_clause = _break(sql, ' FROM [')
             for col in [x.strip() for x in select_list.split(',')]:
                 col_name = re.search(_pat_col, col).group(1)
-                
+                col_key = col_name.lower()
+
                 # If column name was already seen, alias it.
                 if col_name in names_seen:
-                    alias = qn('%s___%s' % (col_name, names_seen.count(col_name)))
+                    alias = qn('%s___%s' % (col_name, names_seen.count(col_key)))
                     outer.append(alias)
                     inner.append("%s as %s" % (col, alias))
                 else:
                     outer.append(qn(col_name))
                     inner.append(col)
 
-                names_seen.append(col_name)
+                names_seen.append(col_key)
 
             # Add FROM clause back to inner select
             return ', '.join(outer), ', '.join(inner) + from_clause

File tests/test_main/regressiontests/models.py

View file
  • Ignore whitespace
     """
     id = models.AutoField(primary_key=True, db_column='bug41id')
     a = models.IntegerField()
-    
+    
+
+class Bug69Table1(models.Model):
+    """
+    Test that pagination works when db_column is specified but the text case
+    does not match between tables.
+    
+    http://code.google.com/p/django-mssql/issues/detail?id=69
+    """
+    # db_column is lowercase here
+    id = models.IntegerField(primary_key=True, db_column='table1id')
+    
+
+class Bug69Table2(models.Model):
+    id = models.IntegerField(primary_key=True, db_column='Table2Id')
+    # db_column is camelcase here
+    related_obj = models.ForeignKey(Bug69Table1, db_column='Table1Id')

File tests/test_main/regressiontests/tests.py

View file
  • Ignore whitespace
 from django.db import models
 from django.test import TestCase
 
+from regressiontests.models import Bug69Table1, Bug69Table2
 
 class Bug38Table(models.Model):
     d = models.DecimalField(max_digits=5, decimal_places=2)
         Bug38Table(d=decimal.Decimal('0450.0')).save()
         d1 = Bug38Table.objects.all()[0]
         self.assertEquals(decimal.Decimal('450.0'), d1.d)
+
+
+class Bug69TestCase(TestCase):
+    def setUp(self):
+        for x in xrange(0,5):
+            Bug69Table2.objects.create(
+                id=x,
+                related_obj=Bug69Table1.objects.create(id=x),
+            )
+        
+    def testConflictingFieldNames(self):
+        objs = list(Bug69Table2.objects.select_related('related_obj')[2:4])
+        self.assertEqual(len(objs), 2)
+
+