Opened 19 years ago
Closed 18 years ago
#1364 closed defect (fixed)
MySQL 5.0.3+ and meta.FloatFields don't mix
Reported by: | 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 , 19 years ago
comment:2 by , 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 , 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 , 19 years ago
MySQL-Python 1.2.1 has been released, including support for the new MySQL-5.0 DECIMAL implementation.
comment:5 by , 18 years ago
Type: | defect |
---|
comment:6 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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.
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?