Opened 19 years ago

Closed 18 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: no UI/UX: no

Description

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/admin_list.py:

- 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 by Adrian Holovaty, 19 years ago

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 by ben@…, 19 years ago

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")
1L
>>> row = c.fetchone()
>>> row[5]
Decimal("2971.80")

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

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 by Malcolm Tredinnick <malcolm@…>, 19 years ago

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 by ben@…, 19 years ago

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

comment:5 by Home, 19 years ago

Type: defect

comment:6 by Thomas Steinacher <tom@…>, 18 years ago

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