ruby-on-rails-3 – 现有列的change_column_null
发布时间:2020-12-17 04:34:40 所属栏目:百科 来源:网络整理
导读:我正在尝试更改现有列上的布尔属性的零可能性::profiles表上的access_titles.由于此迁移,该列存在: class AddAccessItemsToProfiles ActiveRecord::Migration def self.up add_column :profiles,:access_items,:boolean,:default = true end def self.dow
我正在尝试更改现有列上的布尔属性的零可能性::profiles表上的access_titles.由于此迁移,该列存在:
class AddAccessItemsToProfiles < ActiveRecord::Migration def self.up add_column :profiles,:access_items,:boolean,:default => true end def self.down remove_column :profiles,:default => nil end end 要改变nil,我尝试按照以下方式生成迁移: rails g migration ChangeColumnNull :profiles :access_items :null => false 但这没有做任何事情,所以我做了一个独立的迁移: rails g migration AddChangeColumnNullToAccessItems 在那之内我补充说: class AddChangeColumnNullToAccessItems < ActiveRecord::Migration def self.up change_column_null :profiles,false end def self.down change_column_null :profiles,true end end 然后我运行了rake db:migrate,重新启动了我的服务器并且看不到任何更改.所以我尝试过: class AddChangeColumnNullToAccessItems < ActiveRecord::Migration def self.up change_column_null :profiles,true end end 然后做了同样的事情:rake db:migrate,重启服务器,没有任何改变. 我究竟做错了什么?我希望只有布尔值:access_items是true和false而不必转储数据库. 更新:尝试change_column_null:profiles,:access_items,false我收到一个错误: -- change_column_null(:profiles,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 所以,根据下面的建议,我必须在我的迁移中插入change_column_null:profiles,false,true. 解决方法
您可以使用:
change_column_null :profiles,1 第四个参数是可选的,允许您设置列的默认值.当列中有空值并且您将空值设置为false时,这是必需的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |