Opened 12 years ago

Closed 11 years ago

#1364 closed defect (fixed)

MySQL 5.0.3+ and meta.FloatFields don't mix

Reported by: ben@… Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Before MySQL 5.0.3, decimal types were stored unpacked, and behaved like chars; later versions store them as packed, fixed point numbers. More details here. This makes Python's formatting unhappy and a TypeError: Float argument required is raised.

Some Zope folks had trouble with it too. The fix is easy though; just cast field_val to float(), like on line 158 of django/contrib/admin/templatetags/

- result_repr = ('%%.%sf' % f.decimal_places) % field_val
+ result_repr = ('%%.%sf' % f.decimal_places) % float(field_val)

This isn't a data loss bug or anything, but I'm sure it's irritating to anyone using recent MySQL versions.

Change History (6)

comment:1 Changed 12 years ago by Adrian Holovaty

Thanks for reporting this. I'm not clear on the fix, though -- it seems we'd need to change the database wrapper, rather than expecting people to use float() in their client code, no?

comment:2 Changed 12 years ago by ben@…

Yeah, you're right. Upon further investigation, it's the mysql-python bindings that get this wrong.

>>> import MySQLdb
>>> db = MySQLdb.connect(user='test', passwd='test', host='mysql4_box', db='test')
>>> c = db.cursor()
>>> c.execute("select * from finance_accounts")
>>> row = c.fetchone()
>>> row[5]

>>> db = MySQLdb.connect(user='test', passwd='test', host='mysql5_box', db='test')
>>> c = db.cursor()
>>> c.execute("select * from finance_accounts")
>>> row = c.fetchone()
>>> row[5]

The bug was reported a little while ago over at SourceForge; I'm going to ping over there. Hopefully they'll get a fix out soon. You might want to mention this in the docs somewhere, I've seen several hosts upgrading to MySQL 5 and people may get bitten by this problem.

Thanks for the great framework!

comment:3 Changed 11 years ago by Malcolm Tredinnick <malcolm@…>

Just a status update here, since this problem emerged on IRC today: looks like the problem has been fixed upstream (see the SourceForge issue link above) on Feb 26. No release yet, so people wanting an immediate fix will need to use the CVS code from SourceForge.

comment:4 Changed 11 years ago by ben@…

MySQL-Python 1.2.1 has been released, including support for the new MySQL-5.0 DECIMAL implementation.

comment:5 Changed 11 years ago by Home

Type: defect

comment:6 Changed 11 years ago by Thomas Steinacher <tom@…>

Resolution: fixed
Status: newclosed
Type: defect

Closing this, as it has been fixed in MySQL-Python 1.2.1.
Note to debian users: I had "python2.4-mysqldb" installed, which didn't work. I had to install the "python-mysqldb" package to make it work.

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