#7285 closed (fixed)
inspectdb outputs invalid python variable when it encounters a dash
Reported by: | 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 )
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)
Change History (10)
comment:1 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
by , 16 years ago
Attachment: | inspectdb_replace_dash.diff added |
---|
comment:2 by , 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 , 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:5 by , 16 years ago
Description: | modified (diff) |
---|
comment:6 by , 16 years ago
milestone: | → 1.0 |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:7 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:8 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Replaces table name dashes with underscores.