Opened 16 years ago

Last modified 13 years ago

#7285 closed

inspectdb outputs invalid python variable when it encounters a dash — at Version 5

Reported by: redalastor@… Owned by: George Vilches
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.

Change History (6)

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)
Note: See TracTickets for help on using tickets.
Back to Top