Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#7285 closed (fixed)

inspectdb outputs invalid python variable when it encounters a dash

Reported by: redalastor@… Owned by: jdunck
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)

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

Download all attachments as: .zip

Change History (10)

comment:1 Changed 6 years ago by gav

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to gav
  • Patch needs improvement unset
  • Status changed from new to assigned

Changed 6 years ago by jbronn

Replaces table name dashes with underscores.

comment:2 Changed 6 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 6 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 6 years ago by anonymous

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

comment:5 Changed 6 years ago by ramiro

  • Description modified (diff)

comment:6 Changed 6 years ago by ericholscher

  • milestone set to 1.0
  • Triage Stage changed from Unreviewed to Design decision needed

comment:7 Changed 6 years ago by jdunck

  • Owner changed from gav to jdunck
  • Status changed from assigned to new

comment:8 Changed 6 years ago by russellm

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

(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 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.