Django

Code

Ticket #1364 (closed: fixed)

Opened 4 years ago

Last modified 3 years ago

MySQL 5.0.3+ and meta.FloatFields don't mix

Reported by: ben@telesector.net Assigned to: adrian
Milestone: Component: django.contrib.admin
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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.

Attachments

Change History

02/17/06 11:35:54 changed by adrian

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?

02/17/06 13:59:25 changed by ben@telesector.net

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!

03/03/06 20:07:19 changed by Malcolm Tredinnick <malcolm@pointy-stick.com>

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.

04/03/06 13:19:34 changed by ben@telesector.net

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

06/12/06 06:37:35 changed by Home

  • type deleted.

01/01/07 16:26:58 changed by Thomas Steinacher <tom@eggdrop.ch>

  • status changed from new to closed.
  • type set to defect.
  • resolution set to fixed.

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.


Add/Change #1364 (MySQL 5.0.3+ and meta.FloatFields don't mix)




Change Properties
Action