Uninitialized constant after rake db migration - ruby-on-rails

I wanted to add a table called measurement_error_categories using rake db:migrate.
I created a migration file with bundle exec rake db:create_migration NAME=create_measurement_error_category_table
Now I get following error after running bundle exec rake db:migrate
== 20170620084816 CreateMeasurementErrorCategoryTable: migrating ==============
rake aborted!
An error has occurred, this and all later migrations canceled:
uninitialized constant CreateMeasurementErrorCategoryTable::MeasurementErrorCategory
/Users/ndinatale/leanlogic-qa-prototype/server/db/migrate/20170620084816_create_measurement_error_category_table.rb:3:in `change'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:602:in `exec_migration'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:586:in `block (2 levels) in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:585:in `block in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:584:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:759:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/transactions.rb:208:in `transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:1044:in `ddl_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:959:in `block in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:955:in `each'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:955:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:814:in `up'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/migration.rb:792:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/cli/exec.rb:74:in `load'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/cli/exec.rb:74:in `kernel_load'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/cli/exec.rb:27:in `run'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/cli.rb:335:in `exec'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/cli.rb:20:in `dispatch'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/cli.rb:11:in `start'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/exe/bundle:32:in `block in <top (required)>'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
/Library/Ruby/Gems/2.0.0/gems/bundler-1.14.3/exe/bundle:24:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
20170620084816_create_measurement_error_category_table.rb:
class CreateMeasurementErrorCategoryTable < ActiveRecord::Migration
def change
MeasurementErrorCategory.create_table(self, :measurementerrorcategories)
end
end
measurement_error_category.rb:
class MeasurementErrorCategory < ActiveRecord::Base
include ModelBase
include Ordering
# -----
# Table setup
# -----
before_validation { complete_languageblock(false, :name) }
validates :name, language: { complete: :all, minlength: 3 }
# serialize json object (value) as newline delimited string in the database
serialize :name
augment_class_usertag
def self.create_table(m,tn)
m.create_table tn do |t|
t.string :name
t.integer :seqnum
augment_table_usertag t
end
end
end
What have I done wrong?

Okay I found out. In 20170620084816_create_measurement_error_category_table.rb I changed
class CreateMeasurementErrorCategoryTable < ActiveRecord::Migration
def change
MeasurementErrorCategory.create_table(self, :measurementerrorcategories)
end
end
to
class CreateMeasurementErrorCategoryTable < ActiveRecord::Migration
def create
MeasurementErrorCategory.create_table(self, :measurementerrorcategories)
end
end
So I changed the method name from change to create.

Related

PG::UndefinedTable: ERROR: relation "user_profiles" does not exist when trying to run : heroku run rake db:migrate

I have created a web app in rails and I was using sqlite3. Now I am trying to deploy it with heroku which uses pg but I am running into this error:
PG::UndefinedTable: ERROR: relation "user_profiles" does not exist
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `exec'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `block (2 levels) in execute'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.2.2/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.2.2/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.2.2/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:91:in `block in execute'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:718:in `block (2 levels) in log'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:717:in `block in log'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.2.2/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:708:in `log'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:90:in `execute'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql/schema_statements.rb:443:in `add_index'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:890:in `block in method_missing'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:858:in `block in say_with_time'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:858:in `say_with_time'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:879:in `method_missing'
/app/db/migrate/20200327044108_create_user_profiles.rb:10:in `block in change'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:309:in `create_table'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:890:in `block in method_missing'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:858:in `block in say_with_time'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:858:in `say_with_time'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:879:in `method_missing'
/app/db/migrate/20200327044108_create_user_profiles.rb:3:in `change'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:828:in `exec_migration'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:812:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:811:in `block in migrate'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `with_connection'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:810:in `migrate'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1001:in `migrate'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1311:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1362:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:281:in `block in transaction'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:281:in `transaction'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/transactions.rb:212:in `transaction'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1362:in `ddl_transaction'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1310:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1282:in `block in migrate_without_lock'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1281:in `each'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1281:in `migrate_without_lock'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1229:in `block in migrate'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1382:in `with_advisory_lock'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1229:in `migrate'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1061:in `up'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/migration.rb:1036:in `migrate'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:238:in `migrate'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/railties/databases.rake:86:in `block (3 levels) in <main>'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/railties/databases.rake:84:in `each'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.2.2/lib/active_record/railties/databases.rake:84:in `block (2 levels) in <main>'
/app/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `load'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `kernel_load'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:28:in `run'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/cli.rb:463:in `exec'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/cli.rb:27:in `dispatch'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/cli.rb:18:in `start'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/exe/bundle:30:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
/app/vendor/bundle/ruby/2.6.0/gems/bundler-1.17.3/exe/bundle:22:in `<top (required)>'
/app/bin/bundle:104:in `load'
/app/bin/bundle:104:in `<main>'
I've looked around a bit but still cant get it to work. I can see that it has to do with my user_profiles.
My user profiles model:
class UserProfile < ApplicationRecord
belongs_to :user, class_name: 'User', foreign_key: 'user_id'
validates_uniqueness_of :user_id
end
This is my user profile migrate:
class CreateUserProfiles < ActiveRecord::Migration[6.0]
def change
create_table :user_profiles do |t|
t.integer :user_id
t.string :name
t.integer :age
t.string :address
t.string :ptName
t.string :bio
t.timestamps
end
add_foreign_key :user_profiles, :users, column: :user_id
add_index :user_profiles, :user_id, unique: true
end
end
The error is occurring as when i go to view my app through heroku I see this: Application error. So I try and run: heroku run rake db:migrate and the error in the title occurs. I think it might have to do with Pg being more strict with foreign keys. Any suggestions?
From Rails 5 - when we reference a model, index on the foreign_key is automatically created.
Just do it like below -
class CreateUserProfiles < ActiveRecord::Migration[6.0]
def change
create_table :user_profiles do |t|
t.references :user, null: false, foreign_key: true
t.string :name
t.integer :age
t.string :address
t.string :ptName
t.string :bio
t.timestamps
end
end
end

cannot rollback migration with rake db:rollback

I guess this is another reason I should be backing up my work with GitHub, but alas, I have not been.
The migration I want to rollback is this:
class AddAssociationToTables < ActiveRecord::Migration
def change
add_column :articles, :user, :belongs_to
end
end
Which obviously isn't well formed because it seems to have totally removed my article table from the database.
I tried rake db:rollback:status and there is nothing 'missing' or anything.
I'm gonna skip copy-pasting the entire trace.
I tried to make a counteracting migration and run that. This is what I wrote:
class RemoveAssociationFromArticle < ActiveRecord::Migration
def change
remove_column :articles, :user, :belongs_to
end
end
But when I tried to run rake db:migrate with this I got what looks like the same error.
EDIT ok here's the whole trace for the rollback first, and then the migrate attempt
== AddAssociationToTables: reverting =========================================
-- remove_column(:articles, :user, :belongs_to)
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `to_sym' for nil:NilClass/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:215:in `column'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:545:in `block (2 levels) in copy_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:538:in `each'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:538:in `block in copy_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:184:in `create_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:535:in `copy_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:528:in `move_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:521:in `block in alter_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:200:in `transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:520:in `alter_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:463:in `remove_column'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:625:in `block in method_missing'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:597:in `block in say_with_time'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:597:in `say_with_time'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:617:in `method_missing'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:462:in `block in revert'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:461:in `each'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:461:in `revert'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:569:in `exec_migration'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:554:in `block in migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:553:in `migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:709:in `migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/transactions.rb:209:in `transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:1005:in `ddl_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:920:in `block in migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:916:in `each'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:916:in `migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:771:in `down'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:857:in `move'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:753:in `rollback'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/railties/databases.rake:126:in `block (2 levels) in <top (required)>'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)
== RemoveAssociationFromArticle: migrating ===================================
-- remove_column(:articles, :user, :belongs_to)
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `to_sym' for nil:NilClass/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:215:in `column'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:545:in `block (2 levels) in copy_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:538:in `each'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:538:in `block in copy_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:184:in `create_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:535:in `copy_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:528:in `move_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:521:in `block in alter_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:200:in `transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:520:in `alter_table'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:463:in `remove_column'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:625:in `block in method_missing'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:597:in `block in say_with_time'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:597:in `say_with_time'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:617:in `method_missing'
/home/macs/code/my_apps/rails_apps/satisfactory/dec1/whatis/db/migrate/20131202144933_remove_association_from_article.rb:3:in `change'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:571:in `exec_migration'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:554:in `block in migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:553:in `migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:709:in `migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/transactions.rb:209:in `transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:1005:in `ddl_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:920:in `block in migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:916:in `each'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:916:in `migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:764:in `up'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/migration.rb:742:in `migrate'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/activerecord-4.0.1/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
/home/macs/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
The third parameter should be the type of column.
The correct syntax is:
add_column :articles, :user_id, :integer
The :belongs_to parameter may causing conflict with ActiveRecord methods!
If it did not migrate properly in the first place, don't worry about rolling back. Just fix the migration and try to migrate again.
Your migration should be:
add_column :articles, :user, :references
The belongs_to is for in the model class, and not in the migration. If it gets to a point where you can't do anything, you can always recreate the database to start fresh (and lose all data).
$ rake db:drop db:create db:migrate
Start using version control, and read through all of this slowly and carefully at some point in life http://guides.rubyonrails.org/migrations.html

Rails: rake db:migrate failure `verify_readonly_attribute`

This migration worked fine on an initial developer machine but is failing in a second dev environment. Setups are identical as far as we can tell.
Migration:
class AddInventoryItemsCountToProduct < ActiveRecord::Migration
def change
add_column :products, :inventory_items_count, :integer, default: 0
reversible do |dir|
dir.up do
Product.all.each do |p|
p.update_attribute :inventory_items_count, p.inventory_items.length
end
end
end
end
end
Haven't seen this error. Anyone know what's going on?
$ rake db:migrate
== AddInventoryItemsCountToProduct: migrating ================================
-- add_column(:products, :inventory_items_count, :integer, {:default=>0})
-> 0.0112s
rake aborted!
An error has occurred, this and all later migrations canceled:
inventory_items_count is marked as readonly/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/persistence.rb:506:in `verify_readonly_attribute'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/persistence.rb:217:in `update_attribute'
/Users/meltemi/rails/input/myapp/db/migrate/20130823022914_add_inventory_items_count_to_product.rb:8:in `block (3 levels) in change'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:13:in `each'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:13:in `each'
/Users/meltemi/rails/input/myapp/db/migrate/20130823022914_add_inventory_items_count_to_product.rb:7:in `block (2 levels) in change'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:474:in `up'
/Users/meltemi/rails/input/myapp/db/migrate/20130823022914_add_inventory_items_count_to_product.rb:6:in `block in change'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:510:in `block in reversible'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:687:in `execute_block'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:510:in `reversible'
/Users/meltemi/rails/input/myapp/db/migrate/20130823022914_add_inventory_items_count_to_product.rb:5:in `change'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:571:in `exec_migration'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:554:in `block in migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:553:in `migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:709:in `migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in `ddl_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in `block in migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in `each'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in `migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:in `up'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:742:in `migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/bin/ruby_noexec_wrapper:14:in `eval'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247#myapp/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Check this answer: Problem with counter_cache implementation
Also you might to take the 2nd advice: find_each instead of each

rails 3 migration error in change_column type

I am trying to change a column type and getting error. My model is Conf, and column name is xml. I want to change it from :file to :binary. xml at first created as text, then I've changed it to file with a similar migration file very much like this one. I want to do the same thing again and change the type to binary but getting error.
There is migration file:
class ChangeConfXmlColumnToBinary < ActiveRecord::Migration
def up
change_column :confs, :xml, :binary
end
def down
change_column :confs, :xml, :file
end
end
and this is error:
== ChangeConfXmlColumnToBinary: migrating ====================================
-- change_column(:confs, :xml, :binary)
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `to_sym' for nil:NilClass/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/schema_definitions.rb:229:in `column'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:506:in `block (2 levels) in copy_table'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:500:in `each'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:500:in `block in copy_table'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/schema_statements.rb:160:in `create_table'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:498:in `copy_table'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:488:in `move_table'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:481:in `block in alter_table'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:480:in `alter_table'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:441:in `change_column'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:466:in `block in method_missing'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in `block in say_with_time'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in `say_with_time'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:458:in `method_missing'
/home/staj/rails_projects/sample_app/db/migrate/20130826113731_change_conf_xml_column_to_binary.rb:3:in `up'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:410:in `block in migrate'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:389:in `migrate'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:528:in `migrate'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `call'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `ddl_transaction'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:719:in `block in migrate'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `each'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `migrate'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:570:in `up'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/migration.rb:551:in `migrate'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/bin/ruby_noexec_wrapper:14:in `eval'
/home/staj/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
rake db:rollback gives the same error with "-- change_column(:confs, :xml, :text)" at the beginning
It looks like "binary" is a reserved word.
Take a look at this list of reserve words.
The word "binary" is a reserved word in both MySQL & PostgreSQL, so if you plan on using any of these in production you're likely to run into problems.

rake aborted, wrong number of arguments

I'm trying to add a counter_cache to my tag table
when I run my migration to add the column it aborts (but not before adding the column)
I don't understand it, and although it makes the column, it's going stop any future migrations. How do i fix this?
== AddCountToTags: migrating =================================================
-- add_column(:tags, :reports_count, :integer, {:default=>0})
-> 0.1405s
rake aborted!
An error has occurred, all later migrations canceled:
wrong number of arguments (0 for 1)
The trace:
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/counter_cache.rb:17:in `reset_counters'
/Dropbox/Shared/repair/db/migrate/20120120234938_add_count_to_tags.rb:5:in `up'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:314:in `block in migrate'
/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:295:in `measure'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:314:in `migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:397:in `migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:539:in `block (2 levels) in migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:615:in `call'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:615:in `ddl_transaction'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:538:in `block in migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:525:in `each'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:525:in `migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:435:in `up'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:417:in `migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/railties/databases.rake:151:in `block (2 levels) in <top (required)>'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
migration file:
class AddCountToTags < ActiveRecord::Migration
def self.up
add_column :tags, :reports_count, :integer, :default => 0
Tag.reset_counters
Tag.find(:all).each do |t|
t.update_attribute(:reports_count, t.reports.length)
end
end
def self.down
remove_column :tags, :reports_count
end
end
relevant models:
class TagAssignment < ActiveRecord::Base
belongs_to :tag, :counter_cache => :reports_count
belongs_to :report
end
class Tag < ActiveRecord::Base
has_many :tag_assignments, :dependent => :destroy
has_many :reports, :through => :tag_assignments
end
From what I see here you need to pass an id to reset_counters.
it seems that what you want to do should be (untested) done by:
Tag.find_each do |t|
Tag.reset_counters(t.id, :reports)
end

Resources