Opened 17 years ago

Closed 16 years ago

Last modified 13 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: dev
Severity: Keywords: inspectdb
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 16 years ago.
Replaces table name dashes with underscores.

Download all attachments as: .zip

Change History (10)

comment:1 by George Vilches, 16 years ago

Owner: changed from nobody to George Vilches
Status: newassigned

by jbronn, 16 years ago

Attachment: inspectdb_replace_dash.diff added

Replaces table name dashes with underscores.

comment:2 by jbronn, 16 years ago

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 by anonymous, 16 years ago

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 by anonymous, 16 years ago

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

comment:5 by Ramiro Morales, 16 years ago

Description: modified (diff)

comment:6 by Eric Holscher, 16 years ago

milestone: 1.0
Triage Stage: UnreviewedDesign decision needed

comment:7 by Jeremy Dunck, 16 years ago

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

comment:8 by Russell Keith-Magee, 16 years ago

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 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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