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
This commit is contained in:
Ramiro Morales 2012-02-11 20:53:48 +00:00
parent ccc0e122d4
commit a411242e94
3 changed files with 21 additions and 2 deletions

View File

@ -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.")

View File

@ -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 File

@ -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())