Rails Migration Decimal Column: default => 0 override MySQL precision to 0?

I am using Rails 3.0.3 (I do not ask), and when I start the migration for a table with a decimal column and set: default => 0, it again sets the column scale and precision (10.0).

def self.up
 create_table :courses do |t|
  t.integer :user_id
  t.string :name
  t.decimal :distance, :precision => 5, :scale => 2, :default => 0
  t.text :notes

  t.timestamps
 end
end

When I remove the parameter: default => 0 from the migration, the column scaling and precision are correct: (5.2)

I tried to migrate change_column with only the setting: default =>: 0, but the scale and accuracy of the column were reset to (10.0)

change_column :courses, :distance, :decimal, :default => 0.0

I know that I can go into MySQL and correct the accuracy and scale of the column, but I wonder if I am doing something wrong or if this is an error?

Google does not disclose any information, so I think I'm doing something wrong.

+5
4

: t.decimal :distance, :precision => 5, :scale => 2, :default => 0.00

+6

, . mysql , ,

mysql> ALTER TABLE question ADD (price INTEGER);
mysql> ALTER TABLE question DROP price;
mysql> ALTER TABLE question ADD (frig DECIMAL(5,2));
mysql> ALTER TABLE question CHANGE frig price DECIMAL(5,2);
mysql> ALTER TABLE question ALTER status SET DEFAULT '0';
mysql> ALTER TABLE question MODIFY price INTEGER;

: default = > 0.0 #note 0.0, , ..

, .

+2

def change
    change_column :courses , :distance, :decimal, :precision => 5, :scale => 2, :null => false, :default => '0'
end
+1

, , .

class ChangeVisitratioFormatInCampaigns < ActiveRecord::Migration[5.0]
  def change
    reversible do |dir|
        change_table :campaigns do |t|
            dir.up { t.change :visitratio, :decimal, :precision => 5, :scale => 4, :default => 1 }
            dir.down { t.change :visitratio, :integer }
        end
    end
  end
end
0

All Articles