Tôi đang cố gắng thay đổi nil
khả năng của thuộc tính boolean trên cột hiện tại :access_titles
trên bảng :profiles
. cột đó tồn tại vì di cư này:change_column_null cho cột hiện tại
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
Để thay đổi nil
, tôi đã cố gắng tạo ra một sự chuyển đổi như tôi luôn luôn có:
rails g migration ChangeColumnNull :profiles :access_items :null => false
Nhưng điều đó đã không làm gì, vì vậy tôi đã làm một sự chuyển đổi độc lập:
rails g migration AddChangeColumnNullToAccessItems
Và trong vòng mà tôi nói thêm:
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
Sau đó, tôi chạy rake db:migrate
, khởi động lại máy chủ của mình và không thấy thay đổi nào. Vì vậy, tôi đã thử:
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
Sau đó, thực hiện tương tự: rake db:migrate
, khởi động lại máy chủ và không có gì thay đổi.
Tôi đang làm gì sai? Tôi đã hy vọng chỉ có giá trị boolean là :access_items
là đúng và sai mà không phải đổ cơ sở dữ liệu.
UPDATE: Cố change_column_null :profiles, :access_items, false
tôi có một lỗi:
-- 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
Vì vậy, theo các lời khuyên dưới đây, tôi đã phải chèn change_column_null :profiles, :access_items, false, true
vào di cư của tôi.
Sau cùng, tôi phải thêm cột thứ tư. Cảm ơn một lần nữa! – tvalent2
Không biết rằng tham số thứ tư thậm chí còn có thể. Tôi đã nghĩ rằng điều này xảy ra thường xuyên đến nỗi nó sẽ được yêu cầu. –