mirror of
https://github.com/django/django.git
synced 2025-03-31 19:46:42 +00:00
Fixed #18843 -- Replaced more special chars in column names (inspectdb)
Thanks airstrike for the report.
This commit is contained in:
parent
395c6083af
commit
f5ea730dac
@ -1,6 +1,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import keyword
|
import keyword
|
||||||
|
import re
|
||||||
from optparse import make_option
|
from optparse import make_option
|
||||||
|
|
||||||
from django.core.management.base import NoArgsCommand, CommandError
|
from django.core.management.base import NoArgsCommand, CommandError
|
||||||
@ -142,18 +143,16 @@ class Command(NoArgsCommand):
|
|||||||
else:
|
else:
|
||||||
field_params['db_column'] = col_name
|
field_params['db_column'] = col_name
|
||||||
|
|
||||||
if ' ' in new_name:
|
new_name, num_repl = re.subn(r'\W', '_', new_name)
|
||||||
new_name = new_name.replace(' ', '_')
|
if num_repl > 0:
|
||||||
field_notes.append('Field renamed to remove spaces.')
|
field_notes.append('Field renamed to remove unsuitable characters.')
|
||||||
|
|
||||||
if '-' in new_name:
|
|
||||||
new_name = new_name.replace('-', '_')
|
|
||||||
field_notes.append('Field renamed to remove dashes.')
|
|
||||||
|
|
||||||
if new_name.find('__') >= 0:
|
if new_name.find('__') >= 0:
|
||||||
while new_name.find('__') >= 0:
|
while new_name.find('__') >= 0:
|
||||||
new_name = new_name.replace('__', '_')
|
new_name = new_name.replace('__', '_')
|
||||||
field_notes.append("Field renamed because it contained more than one '_' in a row.")
|
if col_name.lower().find('__') >= 0:
|
||||||
|
# Only add the comment if the double underscore was in the original name
|
||||||
|
field_notes.append("Field renamed because it contained more than one '_' in a row.")
|
||||||
|
|
||||||
if new_name.startswith('_'):
|
if new_name.startswith('_'):
|
||||||
new_name = 'field%s' % new_name
|
new_name = 'field%s' % new_name
|
||||||
|
@ -20,8 +20,11 @@ class DigitsInColumnName(models.Model):
|
|||||||
leading_digit = models.CharField(max_length=11, db_column='4extra')
|
leading_digit = models.CharField(max_length=11, db_column='4extra')
|
||||||
leading_digits = models.CharField(max_length=11, db_column='45extra')
|
leading_digits = models.CharField(max_length=11, db_column='45extra')
|
||||||
|
|
||||||
class UnderscoresInColumnName(models.Model):
|
class SpecialColumnName(models.Model):
|
||||||
field = models.IntegerField(db_column='field')
|
field = models.IntegerField(db_column='field')
|
||||||
|
# Underscores
|
||||||
field_field_0 = models.IntegerField(db_column='Field_')
|
field_field_0 = models.IntegerField(db_column='Field_')
|
||||||
field_field_1 = models.IntegerField(db_column='Field__')
|
field_field_1 = models.IntegerField(db_column='Field__')
|
||||||
field_field_2 = models.IntegerField(db_column='__field')
|
field_field_2 = models.IntegerField(db_column='__field')
|
||||||
|
# Other chars
|
||||||
|
prc_x = models.IntegerField(db_column='prc(%) x')
|
||||||
|
@ -59,8 +59,10 @@ class InspectDBTestCase(TestCase):
|
|||||||
self.assertNotIn(" 45extra = models.CharField", output, msg=error_message)
|
self.assertNotIn(" 45extra = models.CharField", output, msg=error_message)
|
||||||
self.assertIn("number_45extra = models.CharField", output)
|
self.assertIn("number_45extra = models.CharField", output)
|
||||||
|
|
||||||
def test_underscores_column_name_introspection(self):
|
def test_special_column_name_introspection(self):
|
||||||
"""Introspection of column names containing underscores (#12460)"""
|
"""Introspection of column names containing special characters,
|
||||||
|
unsuitable for Python identifiers
|
||||||
|
"""
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
call_command('inspectdb', stdout=out)
|
call_command('inspectdb', stdout=out)
|
||||||
output = out.getvalue()
|
output = out.getvalue()
|
||||||
@ -68,3 +70,4 @@ class InspectDBTestCase(TestCase):
|
|||||||
self.assertIn("field_field = models.IntegerField(db_column='Field_')", output)
|
self.assertIn("field_field = models.IntegerField(db_column='Field_')", output)
|
||||||
self.assertIn("field_field_0 = models.IntegerField(db_column='Field__')", output)
|
self.assertIn("field_field_0 = models.IntegerField(db_column='Field__')", output)
|
||||||
self.assertIn("field_field_1 = models.IntegerField(db_column='__field')", output)
|
self.assertIn("field_field_1 = models.IntegerField(db_column='__field')", output)
|
||||||
|
self.assertIn("prc_x = models.IntegerField(db_column='prc(%) x')", output)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user