Opened 10 years ago

Closed 5 years ago

#1328 closed defect (fixed)

Inspectdb generates bad python code if column name is python reserved word

Reported by: spam@… Owned by: adrian
Component: inspectdb Version: 1.2
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


If the legacy database got the field name 'pass' inspectdb generates:

pass = meta.CharField(....)

Proposed solution - renames reserved word cols to pass_field and adds a comment:

pass_field = meta.CharField(db_column='pass', maxlength=135) # Db field was Python reserved word - field renamed

Attachments (1)

management_rename.diff (2.2 KB) - added by spam@… 10 years ago.
Proposed solution

Download all attachments as: .zip

Change History (4)

Changed 10 years ago by spam@…

Proposed solution

comment:1 Changed 10 years ago by adrian

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

(In [2271]) Fixed #1328 -- Improved 'inspectdb' to handle Python reserved words as field names. Also updated docs.

comment:2 Changed 5 years ago by pappjm@…

  • Component changed from Core framework to inspectdb
  • Needs documentation set
  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Version set to 1.2

This is apparently broken again; I just had inspectdb create a model which used a reserved Python keyword ("from") as a field name.

Django 1.2.3
Python 2.6.5

comment:3 Changed 5 years ago by kmtracey

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

(After 4 years it would be better to open a new ticket rather than re-opening one that is so old. This problem was assuredly fixed so whatever behavior you are seeing is due to something new that should get tracked on its own.)

I cannot recreate what you describe. With the current 1.2.X branch code, given a MySQL table:

mysql> describe testme;
| Field | Type         | Null | Key | Default | Extra          |
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| from  | varchar(512) | YES  |     | NULL    |                |
2 rows in set (0.00 sec)

with a column named from, inspectdb produces:

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#     * Rearrange models' order
#     * Make sure each model has one field with primary_key=True
# Feel free to rename the models, but don't rename db_table values or field names.
# Also note: You'll have to insert the output of ' sqlcustom [appname]'
# into your database.

from django.db import models

class Testme(models.Model):
    id = models.IntegerField(primary_key=True)
    from_field = models.CharField(max_length=1536, db_column='from', blank=True) # Field renamed because it was a Python reserved word. Field name made lowercase.
    class Meta:
        db_table = u'testme'

So if you do open a new ticket please provide more details (database, description of the table as per whatever db utility that database supports, exact output from inspectdb) that would help someone figure out what is going on. It is apparently not enough to simply have a column named 'from' in a table.

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