Skip to content

Commit

Permalink
[1.2.X] Fixed #14796 -- Modified order of operations performed on fie…
Browse files Browse the repository at this point in the history
…ld names by the inspectdb command so it doesn't generates model fields with names equal to Python keywords. Thanks pappjm at gmail dot com for the report, mmcnickle for the fix and Alex for simplifying the tests.

Backport of r15296 and r15297 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15301 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Jan 25, 2011
1 parent 5f92367 commit f01e1cd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
11 changes: 7 additions & 4 deletions django/core/management/commands/inspectdb.py
Expand Up @@ -20,7 +20,7 @@ class Command(NoArgsCommand):
def handle_noargs(self, **options):
try:
for line in self.handle_inspection(options):
print line
self.stdout.write("%s\n" % line)
except NotImplementedError:
raise CommandError("Database inspection isn't supported for the currently selected database backend.")

Expand Down Expand Up @@ -66,12 +66,11 @@ def handle_inspection(self, options):
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 keyword.iskeyword(att_name):
att_name += '_field'
comment_notes.append('Field renamed because it was a Python reserved word.')

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

Expand All @@ -97,6 +96,10 @@ def handle_inspection(self, options):
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.')

# Don't output 'id = meta.AutoField(primary_key=True)', because
# that's assumed if it doesn't exist.
Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/inspectdb/__init__.py
@@ -0,0 +1 @@

8 changes: 8 additions & 0 deletions tests/regressiontests/inspectdb/models.py
@@ -0,0 +1,8 @@
from django.db import models


class People(models.Model):
name = models.CharField(max_length=255)

class Message(models.Model):
from_field = models.ForeignKey(People, db_column='from_id')
14 changes: 14 additions & 0 deletions tests/regressiontests/inspectdb/tests.py
@@ -0,0 +1,14 @@
from StringIO import StringIO

from django.core.management import call_command
from django.test import TestCase


class InspectDBTestCase(TestCase):
def test_attribute_name_not_python_keyword(self):
out = StringIO()
call_command('inspectdb', stdout=out)
error_message = "inspectdb generated an attribute name which is a python keyword"
self.assertFalse("from = models.ForeignKey(InspectdbPeople)" in out.getvalue(), msg=error_message)
self.assertTrue("from_field = models.ForeignKey(InspectdbPeople)" in out.getvalue())
out.close()

0 comments on commit f01e1cd

Please sign in to comment.