Opened 9 years ago

Closed 9 years ago

#4048 closed (fixed)

mysql introspection doesn't handle CHAR(n) fields

Reported by: Bill Fenner <fenner@…> Owned by: adrian
Component: Database layer (models, ORM) Version: 0.96
Severity: Keywords: mysql inspectdb
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I have a legacy table that has columns like

CREATE TABLE `foo_info` (
  `foo_tag` int(11) NOT NULL auto_increment,
  `record_type` char(8) default NULL,
  `name_prefix` char(10) default NULL,
  `first_name` char(20) default NULL,

note: CHAR, not VARCHAR.

"python inspectdb" turns these into TextFields since it doesn't know any better. They work much better as CharFields; the attached diff makes that happen.

Attachments (2) (278 bytes) - added by Bill Fenner <fenner@…> 9 years ago.
Patch for db/backends/mysql/ (430 bytes) - added by Bill Fenner <fenner@…> 9 years ago.
A less incorrect diff for

Download all attachments as: .zip

Change History (6)

Changed 9 years ago by Bill Fenner <fenner@…>

Patch for db/backends/mysql/

comment:1 Changed 9 years ago by Simon G. <dev@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Ready for checkin

Changed 9 years ago by Bill Fenner <fenner@…>

A less incorrect diff for

comment:2 Changed 9 years ago by Bill Fenner <fenner@…>

I missed that the STRING -> TextField mapping was done explicitly. I attached a new patch that changes the existing mapping instead of adding a new one at the end.

CharField is a much better match for STRING than TextField is. Neither one will exactly recreate the original schema with "python sql", but STRINGs are much closer to VAR_STRINGs than they are to BLOBs.

comment:3 Changed 9 years ago by mtredinnick

A bit subjective, this one, but I'm inclined to agree that CharField will be more appropriate. The backwards-incompat change is unlikely to bite anybody badly, either.

In future, please make patches from the route of the source tree so that we can see which file is patched (in this case, it's easy enough to work it out, but not always).

comment:4 Changed 9 years ago by mtredinnick

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

(In [5042]) Fixed #4048 -- When introspecting a MySQL database, map CHAR(n) fields to
Django's CharField type, not TextField. Thanks, Bill Fenner.

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