Rails 4 DB Migration Error:nil的未定义方法`to_sym':NilClass

时间:2016-09-04 09:23:50

标签: ruby-on-rails database ruby-on-rails-4 database-migration rails-migrations

我对rails和编写应用程序的过程相对较新。到目前为止,应用程序本身效果很好最近我想迁移这样的东西:(更新)

class ChangeStuffFromTools < ActiveRecord::Migration
  def change
    change_column :tools, :shares, :integer, :default => 0
    change_column :tools, :views, :integer, :default => 0
    change_column :tools, :likes, :integer, :default => 0
    change_column :tools, :favorites, :integer, :default => 0
    change_column :tools, :featured, :boolean, :default => false
  end
end

我收到此错误:

$ rails g migration remove_stuff_from_tools
      invoke  active_record
      create    db/migrate/20160904090608_remove_stuff_from_tools.rb

Jonas@JONAS_PC ~/gitapps/ocubit (master)
$ rake db:migrate
== 20160904090608 RemoveStuffFromTools: migrating =============================
-- remove_column(:tools, :featured, :boolean)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `to_sym' for nil:NilClass
c:/Users/Jonas/gitapps/ocubit/db/migrate/20160904090608_remove_stuff_from_tools.rb:3:in
change'
      c:inmigrate' NoMethodError: undefined method to_sym' for nil:NilClass
      c:/Users/Jonas/gitapps/ocubit/db/migrate/20160904090608_remove_stuff_from_tools.rb:3:in
  change' c:in `migrate' Tasks: TOP => db:migrate (See full trace by running task with --trace)

我怎么可能修复它,我的意思是我需要访问我的数据库来编辑它:)

2 个答案:

答案 0 :(得分:2)

remove_column的语法是:

remove_column table_name, feild name

所以试试:

class RemoveStuffFromTools < ActiveRecord::Migration
  def change
    remove_column :tools, :featured
    remove_column :tools, :shares
    remove_column :tools, :views
    remove_column :tools, :likes
    remove_column :tools, :favorites
  end
end

更新

对于sqlite remove_column不受支持,对于简单的开发db,您可以:

  1. 更新原始create_table_migration并删除不需要的列。

  2. 重新创建您的数据库:rake db:drop然后rake db:create,最后重新rake db:migrate

答案 1 :(得分:1)

如果您需要更改列的默认值或空值,您也可以使用以下方法:

change_column_default(:accounts, :authorized, 1)
change_column_null(:users, :nickname, false)

有关详细信息,请here are more migration methods