Change integer to float column table Rails
Asked Answered
Q

2

14

I need to change t.integer :mark_up to a float How can I do this? I have tried in my terminal rails g migration change_column(:stakes, :mark_up, :float) by keep getting a syntax error near unexpected token ('

Quark answered 17/3, 2015 at 21:31 Comment(0)
V
27

In your terminal:

rails generate migration ChangeMarkUpToFloat

and in the file that is created: db/migrate/2015xxxxxxxxxx/change_mark_up_to_float.rb

edit it to:

class ChangeMarkUpToFloat < ActiveRecord::Migration
  def change
    change_column :stakes, :mark_up, :float
  end
end

and then back in your terminal:

rake db:migrate
Veta answered 18/3, 2015 at 4:23 Comment(2)
This is ok when the app is not in production. When the app is in production, I would recommend following engineyard's guide on zero downtime deploymentKynan
Be careful with using the change method when a migration really isn't reversible. If you ran rake db:migrate:down, how would it know what type to convert to? Consider defining up and down instead.Salcedo
S
0

You can't use rails code (change_column) in your terminal.

What you need to do is first create the migration: rails generate migration ChangeMarkUpType and then put your rails code in the file that was created.

You can read more about migrations here

Saintebeuve answered 17/3, 2015 at 21:37 Comment(3)
ok i did that and defined change as change_column :stake, :mark_up, :float but my schema still shows that :mark_up is an integerQuark
did you run rake db:migrate?Adlei
@NeilMurphy Did you see the migration run? Can you update your question with the code in the migration file?Saintebeuve

© 2022 - 2024 — McMap. All rights reserved.