Rails 迁移使列 null = > true

我最初创建了一个表,其中的 column 为

t.string   "email",  :default => "", :null => false

需求已经改变,现在我需要允许电子邮件为空。如何编写迁移使: null = > true

63719 次浏览

Try:

change_column :table_name, :email, :string, null: true

change_column_null worked perfectly:

change_column_null :users, :email, true

The reverse has a nice option to update existing records (but not set the default) when null is not allowed.