You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Version information:
MYSQL DB version: 5.5.38-0ubuntu0.14.04.1
Rails version: 4.1.0
Ruby version: 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
I created a table with the following migration:
create_table "my_table", force: true do |t|
t.decimal "my_column", default: 0.0
end
When attempting to assign values to the column, the values would be treated as integers. In the MYSQL console, the column was reported as DECIMAL(10,0) with a default of 0. In the rails console, the column was reported as type :integer and acted as one.
No idea what caused this, but setting precision: 10, scale: 2 caused rails to treat the column as a :decimal again.
DECIMAL(10,0) acts like a decimal without the addition of default.
For example, with a table create_table "my_table", force: true do |t| t.decimal "my_column", default: 0.0 t.decimal "my_other_column" end
Creates two DECIMAL(10,0) columns. If you look at the column types in rails, my_column will be :integer, while my_other_column will be :decimal.
Running the following on some MyTable record:
That helped narrow down the timeframe, and turned up this commit:
commit a099e55b678cec463b479c3589cb4dcfdcc837fc
Author: Wolfgang Kölbl <wok@iki.fi>
Date: Tue Nov 22 21:13:43 2011 +0200
Cast MYSQL_TYPE_DECIMAL AND MYSQL_TYPE_NEWDECIMAL to integer if number of decimals is 0
Otherwise aggregate functions like SUM will produce decimals even when summing integers
diff --git a/ext/mysql2/result.c b/ext/mysql2/result.c
index af9fc67..d827f5b 100644
--- a/ext/mysql2/result.c+++ b/ext/mysql2/result.c@@ -237,7 +237,9 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
break;
case MYSQL_TYPE_DECIMAL: // DECIMAL or NUMERIC field
case MYSQL_TYPE_NEWDECIMAL: // Precision math DECIMAL or NUMERIC field (MySQL 5.0.3 and up)
- if (strtod(row[i], NULL) == 0.000000){+ if (fields[i].decimals == 0) {+ val = rb_cstr2inum(row[i], 10);+ } else if (strtod(row[i], NULL) == 0.000000){
val = rb_funcall(cBigDecimal, intern_new, 1, opt_decimal_zero);
}else{
val = rb_funcall(cBigDecimal, intern_new, 1, rb_str_new(row[i], fieldLengths[i]));
Activity
sodabrew commentedon Nov 6, 2014
A decimal field with nothing past the decimal point sounds a lot like an integer. How do you expect this to function instead?
PlaidShirtPat commentedon Nov 6, 2014
DECIMAL(10,0) acts like a decimal without the addition of default.
For example, with a table
create_table "my_table", force: true do |t|
t.decimal "my_column", default: 0.0
t.decimal "my_other_column"
end
Creates two DECIMAL(10,0) columns. If you look at the column types in rails, my_column will be :integer, while my_other_column will be :decimal.
Running the following on some MyTable record:
@my_table_record.update(my_column: 1.01, my_other_column: 1.01)
@my_table_record.my_column == 1
@my_table_record.my_other_column == 1.01
@my_table_record.my_column.is_a?(Integer)
@my_table_record.my_other_column.is_a?(BigDecimal)
I'm not sure why this is, but this is the behavior I am getting
gmile commentedon Nov 21, 2014
I can confirm this issue.
0.3.11
– working0.3.13
– (the one required by rails4.1.x
) not workingI'm checking using the following line:
The
price
column isDECIMAL(10, 2)
.We're using CLUSTRIX MySQL, if that matters.
sodabrew commentedon Nov 22, 2014
That helped narrow down the timeframe, and turned up this commit:
gmile commentedon Nov 22, 2014
@sodabrew fixed in #563