Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#7285 closed (fixed)

inspectdb outputs invalid python variable when it encounters a dash

Reported by: redalastor@… Owned by: Jeremy Dunck
Component: Core (Management commands) Version: master
Severity: Keywords: inspectdb
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Ramiro Morales)

When inspectdb encounters a field with a dash, it outputs it as a variable name which is invalid in python. For instance, when i ran it, I got the line:

buy-back_amount = models.FloatField(null=True, blank=True)

which I had to manually change to:

buy_back_amount = models.FloatField(null=True, blank=True, db_column="buy-back")

This was found in the SVN version of django with a mysql database.

Attachments (1)

inspectdb_replace_dash.diff (722 bytes) - added by jbronn 8 years ago.
Replaces table name dashes with underscores.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 8 years ago by George Vilches

Needs documentation: unset
Needs tests: unset
Owner: changed from nobody to George Vilches
Patch needs improvement: unset
Status: newassigned

Changed 8 years ago by jbronn

Attachment: inspectdb_replace_dash.diff added

Replaces table name dashes with underscores.

comment:2 Changed 8 years ago by jbronn

I attached a patch. Are there any other characters that are allowed in a SQL table name that aren't in a Python variable name?

comment:3 Changed 8 years ago by anonymous

http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

Pretty much anything is allowed. It might even start with a digit.
What about python keywords?

import re, keyword
att_name = re.sub(r'[^a-z0-9_]', '_', row[0].lower())
if keyword.iskeyword(att_name):
    att_name += '_'
elif att_name[0].isdigit():
    att_name = '_' + att_name

And this won't be enough.

You would have to prevent clashes too to make it bulletproof (with other fields as well as Model methods and python __magic__).

I don't think that's worth it.

Stick to dash replacement if that's a common issue. And if you use perl code in your column names you will want to hack the output anyway.

comment:4 Changed 8 years ago by anonymous

#6935 is the same problem with table names and spaces.

comment:5 Changed 8 years ago by Ramiro Morales

Description: modified (diff)

comment:6 Changed 8 years ago by Eric Holscher

milestone: 1.0
Triage Stage: UnreviewedDesign decision needed

comment:7 Changed 8 years ago by Jeremy Dunck

Owner: changed from George Vilches to Jeremy Dunck
Status: assignednew

comment:8 Changed 8 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(In [8404]) Fixed #7285: Improved inspectdb handling of dashes in table and field names. Thanks to redalastor@… for the report and Justin Bronn for the first part of a fix.

comment:9 Changed 5 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

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