Gugubaight
Gugubaight

Reputation: 187

Rails 4 DB Migration Error: undefined method `to_sym' for nil:NilClass

I am relatively new to rails and in the process of coding an application. So far the app itself works great. Lately I wanted to migrate something like this: (Updated)

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

I get this error:

$ 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)

How can I possibly fix it, I mean I somehow need access to my database to edit it :)

Upvotes: 1

Views: 1491

Answers (2)

Ivan Zarea
Ivan Zarea

Reputation: 2174

If you need to change the column's default value or null value, you can use the following methods, too:

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

For more info, here are more migration methods

Upvotes: 1

Nimir
Nimir

Reputation: 5839

The syntax for remove_column is:

remove_column table_name, feild name

So try:

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

Update:

For sqlite remove_column isn't supported, for simple development db you could:

  1. Update your original create_table_migration and remove unwanted columns.

  2. Recreate your db: rake db:drop then rake db:create and finally rake db:migrate

Upvotes: 2

Related Questions