Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8317 -- Corrected the inspectdb management command to properly…

… set `primary_key=True` and `unique=True` on foreign keys. Thanks to bthomas for the report and patch, and to David Gouldin for the tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17451 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ad8ebb7006be7d93f1c6a29770dbca2752488a62 1 parent d02ba7f
Julien Phalip authored February 05, 2012
20  django/core/management/commands/inspectdb.py
@@ -62,15 +62,22 @@ def handle_inspection(self, options):
62 62
                 if ' ' in att_name or '-' in att_name or keyword.iskeyword(att_name) or column_name != att_name:
63 63
                     extra_params['db_column'] = column_name
64 64
 
  65
+                # Add primary_key and unique, if necessary.
  66
+                if column_name in indexes:
  67
+                    if indexes[column_name]['primary_key']:
  68
+                        extra_params['primary_key'] = True
  69
+                    elif indexes[column_name]['unique']:
  70
+                        extra_params['unique'] = True
  71
+
65 72
                 # Modify the field name to make it Python-compatible.
66 73
                 if ' ' in att_name:
67 74
                     att_name = att_name.replace(' ', '_')
68 75
                     comment_notes.append('Field renamed to remove spaces.')
69  
-                    
  76
+
70 77
                 if '-' in att_name:
71 78
                     att_name = att_name.replace('-', '_')
72 79
                     comment_notes.append('Field renamed to remove dashes.')
73  
-                    
  80
+
74 81
                 if column_name != att_name:
75 82
                     comment_notes.append('Field name made lowercase.')
76 83
 
@@ -88,15 +95,8 @@ def handle_inspection(self, options):
88 95
                     extra_params.update(field_params)
89 96
                     comment_notes.extend(field_notes)
90 97
 
91  
-                    # Add primary_key and unique, if necessary.
92  
-                    if column_name in indexes:
93  
-                        if indexes[column_name]['primary_key']:
94  
-                            extra_params['primary_key'] = True
95  
-                        elif indexes[column_name]['unique']:
96  
-                            extra_params['unique'] = True
97  
-
98 98
                     field_type += '('
99  
-                    
  99
+
100 100
                 if keyword.iskeyword(att_name):
101 101
                     att_name += '_field'
102 102
                     comment_notes.append('Field renamed because it was a Python reserved word.')
9  tests/regressiontests/inspectdb/models.py
@@ -6,3 +6,12 @@ class People(models.Model):
6 6
 
7 7
 class Message(models.Model):
8 8
     from_field = models.ForeignKey(People, db_column='from_id')
  9
+
  10
+class PeopleData(models.Model):
  11
+    people_pk = models.ForeignKey(People, primary_key=True)
  12
+    ssn = models.CharField(max_length=11)
  13
+
  14
+class PeopleMoreData(models.Model):
  15
+    people_unique = models.ForeignKey(People, unique=True)
  16
+    license = models.CharField(max_length=255)
  17
+
4  tests/regressiontests/inspectdb/tests.py
@@ -13,4 +13,8 @@ def test_attribute_name_not_python_keyword(self):
13 13
         error_message = "inspectdb generated an attribute name which is a python keyword"
14 14
         self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", out.getvalue(), msg=error_message)
15 15
         self.assertIn("from_field = models.ForeignKey(InspectdbPeople)", out.getvalue())
  16
+        self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)",
  17
+            out.getvalue())
  18
+        self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)",
  19
+            out.getvalue())
16 20
         out.close()

0 notes on commit ad8ebb7

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