Opened 19 years ago

Closed 14 years ago

#1328 closed defect (fixed)

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

Reported by: spam@… Owned by: Adrian Holovaty
Component: django-admin.py 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: no UI/UX: no

Description

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@… 19 years ago.
Proposed solution

Download all attachments as: .zip

Change History (4)

by spam@…, 19 years ago

Attachment: management_rename.diff added

Proposed solution

comment:1 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

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

comment:2 by pappjm@…, 14 years ago

Component: Core frameworkdjango-admin.py inspectdb
Needs documentation: set
Resolution: fixed
Status: closedreopened
Version: 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 by Karen Tracey, 14 years ago

Resolution: fixed
Status: reopenedclosed

(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 'django-admin.py 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