Skip to content

Commit

Permalink
Fixed #8317 -- Corrected the inspectdb management command to properly…
Browse files Browse the repository at this point in the history
… 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
jphalip committed Feb 5, 2012
1 parent d02ba7f commit ad8ebb7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
20 changes: 10 additions & 10 deletions django/core/management/commands/inspectdb.py
Expand Up @@ -62,15 +62,22 @@ def handle_inspection(self, options):
if ' ' in att_name or '-' in att_name or keyword.iskeyword(att_name) or column_name != att_name:
extra_params['db_column'] = column_name

# Add primary_key and unique, if necessary.
if column_name in indexes:
if indexes[column_name]['primary_key']:
extra_params['primary_key'] = True
elif indexes[column_name]['unique']:
extra_params['unique'] = True

# Modify the field name to make it Python-compatible.
if ' ' in att_name:
att_name = att_name.replace(' ', '_')
comment_notes.append('Field renamed to remove spaces.')

if '-' in att_name:
att_name = att_name.replace('-', '_')
comment_notes.append('Field renamed to remove dashes.')

if column_name != att_name:
comment_notes.append('Field name made lowercase.')

Expand All @@ -88,15 +95,8 @@ def handle_inspection(self, options):
extra_params.update(field_params)
comment_notes.extend(field_notes)

# Add primary_key and unique, if necessary.
if column_name in indexes:
if indexes[column_name]['primary_key']:
extra_params['primary_key'] = True
elif indexes[column_name]['unique']:
extra_params['unique'] = True

field_type += '('

if keyword.iskeyword(att_name):
att_name += '_field'
comment_notes.append('Field renamed because it was a Python reserved word.')
Expand Down
9 changes: 9 additions & 0 deletions tests/regressiontests/inspectdb/models.py
Expand Up @@ -6,3 +6,12 @@ class People(models.Model):

class Message(models.Model):
from_field = models.ForeignKey(People, db_column='from_id')

class PeopleData(models.Model):
people_pk = models.ForeignKey(People, primary_key=True)
ssn = models.CharField(max_length=11)

class PeopleMoreData(models.Model):
people_unique = models.ForeignKey(People, unique=True)
license = models.CharField(max_length=255)

4 changes: 4 additions & 0 deletions tests/regressiontests/inspectdb/tests.py
Expand Up @@ -13,4 +13,8 @@ def test_attribute_name_not_python_keyword(self):
error_message = "inspectdb generated an attribute name which is a python keyword"
self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", out.getvalue(), msg=error_message)
self.assertIn("from_field = models.ForeignKey(InspectdbPeople)", out.getvalue())
self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)",
out.getvalue())
self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)",
out.getvalue())
out.close()

0 comments on commit ad8ebb7

Please sign in to comment.