From a411242e947c2c330a87ec5149510114312c8b8d Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sat, 11 Feb 2012 20:53:48 +0000 Subject: [PATCH] Fixed #17676 -- Fixed introspection of column names that start with digit(s). Thanks Gandalfar for the report and patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17509 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/commands/inspectdb.py | 4 ++-- tests/regressiontests/inspectdb/models.py | 4 ++++ tests/regressiontests/inspectdb/tests.py | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index e81f399788..8da0d7e84f 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -101,8 +101,8 @@ class Command(NoArgsCommand): 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.") diff --git a/tests/regressiontests/inspectdb/models.py b/tests/regressiontests/inspectdb/models.py index fac47aa34f..9f815855b6 100644 --- a/tests/regressiontests/inspectdb/models.py +++ b/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') diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py index e2eced576d..944eb64568 100644 --- a/tests/regressiontests/inspectdb/tests.py +++ b/tests/regressiontests/inspectdb/tests.py @@ -18,3 +18,18 @@ class InspectDBTestCase(TestCase): 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())