I'm attempting to change the nil
possibility of a boolean attribute on the existing column :access_titles
on the :profiles
table. That column exists because of this migration:
class AddAccessItemsToProfiles < ActiveRecord::Migration
def self.up
add_column :profiles, :access_items, :boolean, :default => true
end
def self.down
remove_column :profiles, :access_items, :boolean, :default => nil
end
end
To change nil
, I tried generating a migration as I always have:
rails g migration ChangeColumnNull :profiles :access_items :null => false
But that did nothing, so I did a standalone migration:
rails g migration AddChangeColumnNullToAccessItems
And within that I added:
class AddChangeColumnNullToAccessItems < ActiveRecord::Migration
def self.up
change_column_null :profiles, :access_items, :boolean, false
end
def self.down
change_column_null :profiles, :access_items, :boolean, true
end
end
I then ran rake db:migrate
, restarted my server and saw no changes. So I tried:
class AddChangeColumnNullToAccessItems < ActiveRecord::Migration
def self.up
change_column_null :profiles, :access_items, false
end
def self.down
change_column_null :profiles, :access_items, true
end
end
Then did the same thing: rake db:migrate
, restarted the server, and nothing changed.
What am I doing wrong? I was hoping to only have the boolean value of :access_items
be true and false without having to dump the database.
UPDATE: Trying change_column_null :profiles, :access_items, false
I got an error:
-- change_column_null(:profiles, :access_items, false)
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: column "access_items" contains null values
: ALTER TABLE "profiles" ALTER "access_items" SET NOT NULL
So, per the advice below I had to insert change_column_null :profiles, :access_items, false, true
into my migration.
You can use:
change_column_null :profiles, :access_items, false, 1
The fourth parameter is optional and allows you to set the default value for the column. This is required when you have nulls in a column and you're setting the null value to false
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With