_db_create_field_sql has precision and scale arguments in wrong order
pukku - February 5, 2008 - 14:36
| Project: | Drupal |
| Version: | 7.x-dev |
| Component: | mysql database |
| Category: | bug report |
| Priority: | critical |
| Assigned: | Unassigned |
| Status: | closed |
Description
This also affects the PostgreSQL database.
In both includes/database.mysql-common.inc and includes/database.pgsql.inc, the function _db_create_field_sql has the precision and scale arguments in the wrong order for numeric fields. They should be precision then scale.
That is, the lines:
elseif (isset($spec['precision']) && isset($spec['scale'])) {
$sql .= '('. $spec['scale'] .', '. $spec['precision'] .')';
}should be
elseif (isset($spec['precision']) && isset($spec['scale'])) {
$sql .= '('. $spec['precision'] .', '. $spec['scale'] .')';
}I'm sorry I can't roll an official patch — I'm not set up for that right now.

#1
Here you go. The MySQL docs say you're right, but I haven't tested one bit of this myself.
#2
see also http://www.postgresql.org/docs/8.3/interactive/datatype-numeric.html#DAT...
#3
I don't know if I'm really allowed to make this ready to be committed, but the code looks right...
#4
I tested the patch with precision 5, scale 2. The table is now created (where before there was an error) and it all looks good in mysql:
+--------+--------------+------+-----+---------+-------+| foobar | decimal(5,2) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
#5
Is this used anywhere in Drupal core? Where we would need an update function to correct previously miscreated fields?
#6
It's impossible to miscreate a field this way - as mfb tested, MySQL returns an error if the precision is smaller than the scale.
I grepped for "precision" to make sure that we didn't "work around" the bug by switching precision and scale elsewhere, but didn't find anything.
#7
Great, thanks, committed to 6.x. Still needs to be committed to 7.x.
#8
Committed to CVS HEAD as well -- thanks folks and good catch.
#9
Automatically closed -- issue fixed for two weeks with no activity.