-
Notifications
You must be signed in to change notification settings - Fork 550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Column created as :decimal being treated as :integer on mysql with default: 0.0 #557
Comments
A decimal field with nothing past the decimal point sounds a lot like an integer. How do you expect this to function instead? |
DECIMAL(10,0) acts like a decimal without the addition of default. For example, with a table 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.
I'm not sure why this is, but this is the behavior I am getting |
I can confirm this issue.
I'm checking using the following line: ActiveRecord::Base.connection.exec_query('SELECT price FROM products WHERE product_id 1;') The We're using CLUSTRIX MySQL, if that matters. |
That helped narrow down the timeframe, and turned up this commit: commit a099e55b678cec463b479c3589cb4dcfdcc837fc
Author: Wolfgang Kölbl <[email protected]>
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])); |
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.
The text was updated successfully, but these errors were encountered: