Django

Code

Ticket #7285 (closed: fixed)

Opened 7 months ago

Last modified 4 months ago

inspectdb outputs invalid python variable when it encounters a dash

Reported by: redalastor@gmail.com Assigned to: jdunck
Milestone: 1.0 Component: django-admin.py
Version: SVN Keywords: inspectdb
Cc: Triage Stage: Design decision needed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

inspectdb_replace_dash.diff (0.7 kB) - added by jbronn on 06/10/08 11:49:01.
Replaces table name dashes with underscores.

Change History

06/10/08 11:15:59 changed by gav

  • owner changed from nobody to gav.
  • needs_better_patch changed.
  • status changed from new to assigned.
  • needs_tests changed.
  • needs_docs changed.

06/10/08 11:49:01 changed by jbronn

  • attachment inspectdb_replace_dash.diff added.

Replaces table name dashes with underscores.

06/10/08 11:49:35 changed 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?

06/10/08 18:59:16 changed 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.

06/15/08 06:31:23 changed by anonymous

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

06/16/08 10:56:23 changed by ramiro

  • description changed.

08/08/08 16:09:44 changed by ericholscher

  • stage changed from Unreviewed to Design decision needed.
  • milestone set to 1.0.

08/15/08 19:31:08 changed by jdunck

  • owner changed from gav to jdunck.
  • status changed from assigned to new.

08/16/08 00:03:41 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #7285 (inspectdb outputs invalid python variable when it encounters a dash)




Change Properties
Action