Xullnn
Xullnn

Reputation: 405

How to simultaneously change a column's type and default value? Ruby on Rails

the "Products" table in schema

In db/schema the table "products" includes a column name "quantity" , its type is string . Now I want to change its type to integer, and set a default value 1.

I did it by two steps :

step 1

rails g migration change_products_quantity_column_type

fill in change_column :products, :quantity, :integer

then rake db:migrate

step2

rails g migration set_default_value_to_quantity

fill in change_column_default :products, :quantity, 1

then rake db:migrate

Can I do this with a single step ? I mean combine the two steps above.

Upvotes: 0

Views: 475

Answers (1)

pan.goth
pan.goth

Reputation: 1485

change_column has the options parameter, so you can do this in one step, like this:

change_column :products, :quantity, :integer, default: 1

Upvotes: 3

Related Questions