Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#14796 closed (fixed)

inspectdb producing field names which are Python keywords

Reported by: pappjm@… Owned by: mmcnickle
Component: django-admin.py inspectdb Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Similar to #1328; I had a case where inspectdb produced a model which uses a python keyword as a field in the model class.

  • Django: 1.2.3
  • Python: 2.6.5
  • DB Driver: postgresql_psycopg2

I will attach an SQL file; it is from the Danbooru image server. The table which had the problems is "dmails"; the column is a foreign key column to the table "users".

Attachments (4)

danbooru.sql (74.0 KB) - added by pappjm@… 4 years ago.
Schema of the database which inspectdb is failing on
minimal.sql (911 bytes) - added by mmcnickle 4 years ago.
Minimal MySQL schema to reproduce problem
move_keyword_check.diff (1.4 KB) - added by mmcnickle 4 years ago.
Move the keyword check after the relations check/modifications
14796-move_keyword_check_with_tests.diff (3.9 KB) - added by mmcnickle 4 years ago.
Improved the test by using the pre-existing stdout capture method, updated inspectdb.py to take advantage of it.

Download all attachments as: .zip

Change History (9)

Changed 4 years ago by pappjm@…

Schema of the database which inspectdb is failing on

comment:1 Changed 4 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

As suspected, this happens because one transformation made to field names after the verification against the Python keyword list (namely the striping of '_id' from e.g. 'from_id') can result in a 'from' introspected model field name. There are a couple of that kind of transformations.

comment:2 Changed 4 years ago by mmcnickle

  • Owner changed from nobody to mmcnickle
  • Status changed from new to assigned

Changed 4 years ago by mmcnickle

Minimal MySQL schema to reproduce problem

Changed 4 years ago by mmcnickle

Move the keyword check after the relations check/modifications

comment:3 Changed 4 years ago by mmcnickle

  • Has patch set

Changed 4 years ago by mmcnickle

Improved the test by using the pre-existing stdout capture method, updated inspectdb.py to take advantage of it.

comment:4 Changed 4 years ago by ramiro

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

Fixed in r15296.

comment:5 Changed 4 years ago by ramiro

(In [15301]) [1.2.X] Fixed #14796 -- Modified order of operations performed on field names by the inspectdb command so it doesn't generates model fields with names equal to Python keywords. Thanks pappjm at gmail dot com for the report, mmcnickle for the fix and Alex for simplifying the tests.

Backport of r15296 and r15297 from trunk.

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