Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9736 -- Added quoting to the SQL constraint names generated du…

…ring table creation. This is to accommodate primary keys with spaces.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9543 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5834b1837faf6d1711f40d948c0ca81a1d4899c7 1 parent 87fc7ef
Russell Keith-Magee authored December 02, 2008
4  django/db/backends/creation.py
@@ -126,7 +126,7 @@ def sql_for_pending_references(self, model, style, pending_references):
126 126
                 # So we are careful with character usage here.
127 127
                 r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table))))
128 128
                 final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \
129  
-                    (qn(r_table), truncate_name(r_name, self.connection.ops.max_name_length()),
  129
+                    (qn(r_table), qn(truncate_name(r_name, self.connection.ops.max_name_length())),
130 130
                     qn(r_col), qn(table), qn(col),
131 131
                     self.connection.ops.deferrable_sql()))
132 132
             del pending_references[model]
@@ -186,7 +186,7 @@ def sql_for_many_to_many_field(self, model, f, style):
186 186
                         abs(hash((r_table, table))))
187 187
                 output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' %
188 188
                 (qn(r_table),
189  
-                truncate_name(r_name, self.connection.ops.max_name_length()),
  189
+                qn(truncate_name(r_name, self.connection.ops.max_name_length())),
190 190
                 qn(r_col), qn(table), qn(col),
191 191
                 self.connection.ops.deferrable_sql()))
192 192
 
0  tests/regressiontests/custom_columns_regress/__init__.py
No changes.
100  tests/regressiontests/custom_columns_regress/models.py
... ...
@@ -0,0 +1,100 @@
  1
+"""
  2
+Regression for #9736.
  3
+
  4
+Checks some pathological column naming to make sure it doesn't break
  5
+table creation or queries.
  6
+
  7
+"""
  8
+
  9
+from django.db import models
  10
+
  11
+class Article(models.Model):
  12
+    Article_ID = models.AutoField(primary_key=True, db_column='Article ID')
  13
+    headline = models.CharField(max_length=100)
  14
+    authors = models.ManyToManyField('Author', db_table='my m2m table')
  15
+    primary_author = models.ForeignKey('Author', db_column='Author ID', related_name='primary_set')
  16
+
  17
+    def __unicode__(self):
  18
+        return self.headline
  19
+
  20
+    class Meta:
  21
+        ordering = ('headline',)
  22
+
  23
+class Author(models.Model):
  24
+    Author_ID = models.AutoField(primary_key=True, db_column='Author ID')
  25
+    first_name = models.CharField(max_length=30, db_column='first name')
  26
+    last_name = models.CharField(max_length=30, db_column='last name')
  27
+
  28
+    def __unicode__(self):
  29
+        return u'%s %s' % (self.first_name, self.last_name)
  30
+
  31
+    class Meta:
  32
+        db_table = 'my author table'
  33
+        ordering = ('last_name','first_name')
  34
+
  35
+
  36
+__test__ = {'API_TESTS':"""
  37
+# Create a Author.
  38
+>>> a = Author(first_name='John', last_name='Smith')
  39
+>>> a.save()
  40
+
  41
+>>> a.Author_ID
  42
+1
  43
+
  44
+# Create another author
  45
+>>> a2 = Author(first_name='Peter', last_name='Jones')
  46
+>>> a2.save()
  47
+
  48
+# Create an article
  49
+>>> art = Article(headline='Django lets you build web apps easily', primary_author=a)
  50
+>>> art.save()
  51
+>>> art.authors = [a, a2]
  52
+
  53
+# Although the table and column names on Author have been set to custom values,
  54
+# nothing about using the Author model has changed...
  55
+
  56
+# Query the available authors
  57
+>>> Author.objects.all()
  58
+[<Author: Peter Jones>, <Author: John Smith>]
  59
+
  60
+>>> Author.objects.filter(first_name__exact='John')
  61
+[<Author: John Smith>]
  62
+
  63
+>>> Author.objects.get(first_name__exact='John')
  64
+<Author: John Smith>
  65
+
  66
+>>> Author.objects.filter(firstname__exact='John')
  67
+Traceback (most recent call last):
  68
+    ...
  69
+FieldError: Cannot resolve keyword 'firstname' into field. Choices are: Author_ID, article, first_name, last_name, primary_set
  70
+
  71
+>>> a = Author.objects.get(last_name__exact='Smith')
  72
+>>> a.first_name
  73
+u'John'
  74
+>>> a.last_name
  75
+u'Smith'
  76
+>>> a.firstname
  77
+Traceback (most recent call last):
  78
+    ...
  79
+AttributeError: 'Author' object has no attribute 'firstname'
  80
+>>> a.last
  81
+Traceback (most recent call last):
  82
+    ...
  83
+AttributeError: 'Author' object has no attribute 'last'
  84
+
  85
+# Although the Article table uses a custom m2m table,
  86
+# nothing about using the m2m relationship has changed...
  87
+
  88
+# Get all the authors for an article
  89
+>>> art.authors.all()
  90
+[<Author: Peter Jones>, <Author: John Smith>]
  91
+
  92
+# Get the articles for an author
  93
+>>> a.article_set.all()
  94
+[<Article: Django lets you build web apps easily>]
  95
+
  96
+# Query the authors across the m2m relation
  97
+>>> art.authors.filter(last_name='Jones')
  98
+[<Author: Peter Jones>]
  99
+
  100
+"""}

0 notes on commit 5834b18

Please sign in to comment.
Something went wrong with that request. Please try again.