Code

Opened 7 years ago

Closed 7 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:

Description

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 manage.py 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)

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

Download all attachments as: .zip

Change History (6)

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

Patch for db/backends/mysql/introspection.py

comment:1 Changed 7 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 7 years ago by Bill Fenner <fenner@…>

A less incorrect diff for introspection.py

comment:2 Changed 7 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 manage.py sql", but STRINGs are much closer to VAR_STRINGs than they are to BLOBs.

comment:3 Changed 7 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 7 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.