【问题标题】:Rails Migration to make a column null => trueRails 迁移使列为 null => true
【发布时间】:2012-06-09 16:12:56
【问题描述】:

我最初创建了一个列为的表

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

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

【问题讨论】:

标签: ruby-on-rails-3 migration


【解决方案1】:

我无法让上述解决方案与 Active Record 4.0.8 和 Postgresql 9.3 一起使用

但是change_column_null 工作得很好。

change_column_null :users, :email, true

当不允许 null 时,反向有一个不错的选项来更新现有记录(但不设置默认值)。

【讨论】:

  • 如果您希望 null 成为默认列值,我建议您同时使用 change_column_default。否则它将是false0
【解决方案2】:

试试:

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

【讨论】:

  • 请考虑使用change_column_null,因为此change_column 在某些版本中可能不起作用
猜你喜欢
  • 2012-03-22
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 2011-02-17
相关资源
最近更新 更多