Reputation: 5009
I've seen a few questions (namely this one) here on SO about adding a default boolean value to an existing column. So I tried the change_column
suggestion but I mustn't be doing it right.
I tried:
$ change_column :profiles, :show_attribute, :boolean, :default => true
Which returns -bash: change_column: command not found
I then ran:
$ rails g change_column :profiles, :show_attribute, :boolean, :default => true
...and
$ rails change_column :profiles, :show_attribute, :boolean, :default => true
Then ran rake db:migrate
, but the value for :show_attribute
remained nil
. In the question I referenced above it says in PostgreSQL you need to update it manually. Since I'm using PostgreSQL I added the following in my create_profiles
migration:
t.boolean :show_attribute, :default => true
Can someone tell me what I'm doing wrong here?
Upvotes: 166
Views: 149183
Reputation: 21884
change_column
is a method of ActiveRecord::Migration
, so you can't call it like that in the console.
If you want to add a default value for this column, create a new migration:
rails g migration add_default_value_to_show_attribute
Then in the migration created:
# That's the more generic way to change a column
def up
change_column :profiles, :show_attribute, :boolean, default: true
end
def down
change_column :profiles, :show_attribute, :boolean, default: nil
end
OR a more specific option:
def up
change_column_default :profiles, :show_attribute, true
end
def down
change_column_default :profiles, :show_attribute, nil
end
Then run rake db:migrate
.
It won't change anything to the already created records. To do that you would have to create a rake task
or just go in the rails console
and update all the records (which I would not recommend in production).
When you added t.boolean :show_attribute, :default => true
to the create_profiles
migration, it's expected that it didn't do anything. Only migrations that have not already been ran are executed. If you started with a fresh database, then it would set the default to true.
Upvotes: 324
Reputation: 678
If you don't want to create another migration-file for a small, recent change - from Rails Console:
ActiveRecord::Migration.change_column :profiles, :show_attribute, :boolean, :default => true
Then exit and re-enter rails console, so DB-Changes will be in-effect. Then, if you do this ...
Profile.new()
You should see the "show_attribute" default-value as true.
For existing records, if you want to preserve existing "false" settings and only update "nil" values to your new default:
Profile.all.each{|profile| profile.update_attributes(:show_attribute => (profile.show_attribute == nil ? true : false)) }
Update the migration that created this table, so any future builds of the DB will get it right from the onset. Also run the same process on any deployed-instances of the DB.
If using the "new db migration" method, you can do the update of existing nil-values in that migration.
Upvotes: 0
Reputation: 1308
If you just made a migration, you can rollback and then make your migration again.
To rollback you can do as many steps as you want:
rake db:rollback STEP=1
Or, if you are using Rails 5.2 or newer:
rails db:rollback STEP=1
Then, you can just make the migration again:
def change
add_column :profiles, :show_attribute, :boolean, default: true
end
Don't forget to rake db:migrate
and if you are using heroku heroku run rake db:migrate
Upvotes: 2
Reputation: 101
Also, as per the doc:
default cannot be specified via command line
https://guides.rubyonrails.org/active_record_migrations.html
So there is no ready-made rails generator. As specified by above answers, you have to fill manually your migration file with the change_column_default
method.
You could create your own generator: https://guides.rubyonrails.org/generators.html
Upvotes: 1
Reputation: 23939
change_column :things, :price_1, :integer, default: 123, null: false
Seems to be best way to add a default to an existing column that doesn't have null: false
already.
Otherwise:
change_column :things, :price_1, :integer, default: 123
Some research I did on this:
https://gist.github.com/Dorian/417b9a0e1a4e09a558c39345d50c8c3b
Upvotes: 0
Reputation: 3568
I'm not sure when this was written, but currently to add or remove a default from a column in a migration, you can use the following:
change_column_null :products, :name, false
Rails 5:
change_column_default :products, :approved, from: true, to: false
http://edgeguides.rubyonrails.org/active_record_migrations.html#changing-columns
Rails 4.2:
change_column_default :products, :approved, false
http://guides.rubyonrails.org/v4.2/active_record_migrations.html#changing-columns
Which is a neat way of avoiding looking through your migrations or schema for the column specifications.
Upvotes: 37
Reputation: 1346
As a variation on the accepted answer you could also use the change_column_default
method in your migrations:
def up
change_column_default :profiles, :show_attribute, true
end
def down
change_column_default :profiles, :show_attribute, nil
end
Upvotes: 96