I am trying to change the nil
capability of a boolean attribute in an existing column :access_titles
in a table :profiles
. This column exists due to 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 to create a migration, as always:
rails g migration ChangeColumnNull :profiles :access_items :null => false
But it didn’t do anything, so I did a separate migration:
rails g migration AddChangeColumnNullToAccessItems
And in this 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
Then I ran rake db:migrate
, restarted my server and did not see any 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 he did the same: rake db:migrate
, restarted the server and nothing changed.
What am I doing wrong? I was hoping that only the boolean value :access_items
would be true and false without having to reset 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, according to the advice below, I had to insert change_column_null :profiles, :access_items, false, true
in my migration.