Opened 4 years ago

Closed 4 years ago

#17676 closed Bug (fixed)

inspectdb generates invalid field name for tables that start with a number

Reported by: Gandalfar Owned by: nobody
Component: Core (Management commands) Version: master
Severity: Normal Keywords: inspectdb
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Ticket #16536 introduces patch that fixes this for fields that are numbers. It turns out that some people start their table names with numbers (e.g.: '3starost') and Django doesn't like that.

This patch extends current functionality by making sure that first character of table isn't a digit:

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

Original patch didn't have a test and I'm not sure how actually test this within Django framework, but I'm happy to write one I someone can give me hint where to start, if it's required.

Attachments (1)

ticket17676.diff (829 bytes) - added by Gandalfar 4 years ago.

Download all attachments as: .zip

Change History (3)

Changed 4 years ago by Gandalfar

comment:1 Changed 4 years ago by ramiro

  • Keywords inspectdb added
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 years ago by ramiro

  • Resolution set to fixed
  • Status changed from new to closed

In [17509]:

Fixed #17676 -- Fixed introspection of column names that start with digit(s).

Thanks Gandalfar for the report and patch.

Note: See TracTickets for help on using tickets.
Back to Top