Opened 10 years ago

Closed 10 years ago

#4048 closed (fixed)

mysql introspection doesn't handle CHAR(n) fields

Reported by: Bill Fenner <fenner@…> Owned by: Adrian Holovaty
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@…> 10 years ago.
Patch for db/backends/mysql/introspection.py
introspection.py-try-2.diff (430 bytes) - added by Bill Fenner <fenner@…> 10 years ago.
A less incorrect diff for introspection.py

Download all attachments as: .zip

Change History (6)

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

Attachment: introspection.py.diff added

Patch for db/backends/mysql/introspection.py

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

Triage Stage: UnreviewedReady for checkin

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

Attachment: introspection.py-try-2.diff added

A less incorrect diff for introspection.py

comment:2 Changed 10 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 10 years ago by Malcolm Tredinnick

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 10 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(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