1

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

Gugubaight
  • 187
  • 13

2 Answers2

2

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

Nimir
  • 5,727
  • 1
  • 26
  • 34
  • Hmm, that's what I tried before - the same error pops up. Thanks for your answer though. – Gugubaight Sep 04 '16 at 09:36
  • @J.Hübotter are you using sqlite? re-check column names spelling – Nimir Sep 04 '16 at 09:43
  • yes, nothing changed from the default, the columns are spelled correctly – Gugubaight Sep 04 '16 at 09:46
  • i don't think sqlite support `remove_column` http://stackoverflow.com/questions/8442147/how-to-delete-or-add-column-in-sqlite . there are some hacky ways http://stackoverflow.com/questions/5938048/delete-column-from-sqlite-table/5987838#5987838 but should drop and recreate if it's in devlopment – Nimir Sep 04 '16 at 09:51
  • Is there an easier way to just update these columns? – Gugubaight Sep 04 '16 at 09:55
  • you can edit your migrations and remove unwanted columns from the `create_table_migration` then do `rake db:drop` then `rake db:create` and finally `rake db:migrate` – Nimir Sep 04 '16 at 09:57
  • You are a genius! Didn't thought about that solution. Feel free to update your post, I'll mark this as answered then :) – Gugubaight Sep 04 '16 at 10:15
  • One should back up data say into a CSV file or generate insert statements to bring it back because rake db:drop will remove all the data. – Arthur Apr 21 '17 at 03:38
1

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

Ivan Zarea
  • 2,174
  • 15
  • 13