Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #21079 -- Further normalized table names in inspectdb #2885

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion django/core/management/commands/inspectdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def handle_inspection(self, options):
# 'table_name_filter' is a stealth option
table_name_filter = options.get('table_name_filter')

table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '')
table2model = lambda table_name: re.sub(r'[^a-zA-Z0-9]', '', table_name.title())
strip_prefix = lambda s: s[1:] if s.startswith("u'") else s

with connection.cursor() as cursor:
Expand Down
5 changes: 4 additions & 1 deletion tests/inspectdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DigitsInColumnName(models.Model):
leading_digits = models.CharField(max_length=11, db_column='45extra')


class SpecialColumnName(models.Model):
class SpecialName(models.Model):
field = models.IntegerField(db_column='field')
# Underscores
field_field_0 = models.IntegerField(db_column='Field_')
Expand All @@ -40,6 +40,9 @@ class SpecialColumnName(models.Model):
prc_x = models.IntegerField(db_column='prc(%) x')
non_ascii = models.IntegerField(db_column='tamaño')

class Meta:
db_table = "inspectdb_special.table name"


class ColumnTypes(models.Model):
id = models.AutoField(primary_key=True)
Expand Down
16 changes: 15 additions & 1 deletion tests/inspectdb/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def test_special_column_name_introspection(self):
unsuitable for Python identifiers
"""
out = StringIO()
call_command('inspectdb', stdout=out)
call_command('inspectdb',
table_name_filter=lambda tn: tn.startswith('inspectdb_'),
stdout=out)
output = out.getvalue()
base_name = 'Field' if not connection.features.uppercases_column_names else 'field'
self.assertIn("field = models.IntegerField()", output)
Expand All @@ -193,6 +195,18 @@ def test_special_column_name_introspection(self):
else:
self.assertIn("tama_o = models.IntegerField(db_column='tama\\xf1o')", output)

def test_table_name_introspection(self):
"""
Introspection of table names containing special characters,
unsuitable for Python identifiers
"""
out = StringIO()
call_command('inspectdb',
table_name_filter=lambda tn: tn.startswith('inspectdb_'),
stdout=out)
output = out.getvalue()
self.assertIn("class InspectdbSpecialTableName(models.Model):", output)

def test_managed_models(self):
"""Test that by default the command generates models with `Meta.managed = False` (#14305)"""
out = StringIO()
Expand Down