Reputation: 187
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
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
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
For sqlite remove_column
isn't supported, for simple development db you could:
Update your original create_table_migration
and remove unwanted columns.
Recreate your db: rake db:drop
then rake db:create
and finally rake db:migrate
Upvotes: 2