views:

385

answers:

3

This seems to have been asked before: http://stackoverflow.com/questions/1402547/rails-decimal-precision-and-scale

But when running a change_column migration for :precision or :scale they don't actually affect the schema or database, but db:migrate runs without errors.

My migration file looks like this:

class ChangePrecisionAndScaleOfPaybackPeriodInTags < ActiveRecord::Migration
  def self.up
    change_column :tags, :payback_period, :decimal, { :scale => 3, :precision => 10 }
  end

  def self.down
    change_column :tags, :payback_period, :decimal
  end
end

But my schema (and the data) remains as:

t.decimal  "rate"  # previous column
t.decimal  "payback_period"
t.string   "component_type"  # next column

Anybody else have this issue?

Thanks,

Josh

A: 

Delete and regenerate db\schema.rb file.

rake db:schema:dump
KandadaBoggu
No joy, still shows same thing. Plus, using the app shows that the migration options have had no effect.
Josh Pinter
I ran the same script and `schema.rb` file changed accordingly.
KandadaBoggu
I tried it again and still nothing. Perhaps having to do with content already in the database?
Josh Pinter
A: 

A hack, but it should get you where you need to go:

class ChangePrecisionAndScaleOfPaybackPeriodInTags < ActiveRecord::Migration
  def self.up
    execute "ALTER TABLE tags CHANGE payback_period DECIMAL(3,10)"
  end

  def self.down
    change_column :tags, :payback_period, :decimal
  end
end
Benson
Good suggestion, but this didn't work either. I went rooting around trying to find proper syntax for altering a column type in sqlite3 and found there is no way. I'm sure with mysql or another db, I wouldn't even have a problem. Posting an answer here.
Josh Pinter
A: 

For this simple test app that I'm running I have SQLite3 setup. Apparently, SQLite3 doesn't rely on column type declarations and is more dynamic, looking at the column's content instead - as was stumbled upon here:

http://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3

I haven't tested it but I'm sure that's why the schema wasn't being changed, because change_column doesn't translate to anything in SQLite3.

Thanks for the replies guys.

J

Josh Pinter