Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #17676 -- Fixed introspection of column names that start with d…

…igit(s).

Thanks Gandalfar for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17509 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a411242e947c2c330a87ec5149510114312c8b8d 1 parent ccc0e12
@ramiro ramiro authored
View
4 django/core/management/commands/inspectdb.py
@@ -101,8 +101,8 @@ def handle_inspection(self, options):
att_name += '_field'
comment_notes.append('Field renamed because it was a Python reserved word.')
- if att_name.isdigit():
- att_name = 'number_%d' % int(att_name)
+ if att_name[0].isdigit():
+ att_name = 'number_%s' % att_name
extra_params['db_column'] = unicode(column_name)
comment_notes.append("Field renamed because it wasn't a "
"valid Python identifier.")
View
4 tests/regressiontests/inspectdb/models.py
@@ -15,3 +15,7 @@ class PeopleMoreData(models.Model):
people_unique = models.ForeignKey(People, unique=True)
license = models.CharField(max_length=255)
+class DigitsInColumnName(models.Model):
+ all_digits = models.CharField(max_length=11, db_column='123')
+ leading_digit = models.CharField(max_length=11, db_column='4extra')
+ leading_digits = models.CharField(max_length=11, db_column='45extra')
View
15 tests/regressiontests/inspectdb/tests.py
@@ -18,3 +18,18 @@ def test_attribute_name_not_python_keyword(self):
self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)",
out.getvalue())
out.close()
+
+ def test_digits_column_name_introspection(self):
+ """Introspection of column names consist/start with digits (#16536/#17676)"""
+ out = StringIO()
+ call_command('inspectdb', stdout=out)
+ error_message = "inspectdb generated a model field name which is a number"
+ self.assertNotIn(" 123 = models.CharField", out.getvalue(), msg=error_message)
+ self.assertIn("number_123 = models.CharField", out.getvalue())
+
+ error_message = "inspectdb generated a model field name which starts with a digit"
+ self.assertNotIn(" 4extra = models.CharField", out.getvalue(), msg=error_message)
+ self.assertIn("number_4extra = models.CharField", out.getvalue())
+
+ self.assertNotIn(" 45extra = models.CharField", out.getvalue(), msg=error_message)
+ self.assertIn("number_45extra = models.CharField", out.getvalue())

0 comments on commit a411242

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