PG::UndefinedTable: ERROR: relation "step_images" does not exist - ruby-on-rails

On migration I get the following error message:
PG::UndefinedTable: ERROR: relation "step_images" does not exist LINE 5: WHERE a.attrelid = '"step_images"'::regclass ^ : SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"step_images"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
in db
class CreateStepImages < ActiveRecord::Migration
def change
create_table :step_images do |t|
t.string :text
t.attachment :image1
t.attachment :image2
t.attachment :image3
t.attachment :image4
t.attachment :image5
t.references :steptation, index: true
t.timestamps
end
end
end
in step_image model
class StepImage < ActiveRecord::Base
belongs_to :steptation
belongs_to :user
end
in steptation model
has_many :step_images
in step_images controller
class StepImagesController < ApplicationController
before_action :find_step_image ,only:[:show,:update,:edit,:destroy]
before_action :set_steptation, only: [:new, :create]
def new
#steptation =Steptation.find(params[:id])
#step_image = #steptation.step_images.build
#user = current_user
end
def index
end
def create
#step_image = Step_image.new(step_image_params)
#step_image.user_id = current_user.id
if #step_image.save
#steptation.step_images << #step_image
redirect_to case_path(id: #steptation.case_id)
else
render :new
end
end
def edit
end
def update
if #step_image.update(step_image_params)
redirect_to root_path, alert: "steptation Information updated successfully"
else
flash.now[:error] = "Couldn't update!"
render :edit
end
end
def destroy
#step_image.destroy
redirect_to root_path, notice: "step_image deleted Successfully"
end
private
def set_steptation
#steptation = Steptation.find(params[:id])
end
def find_step_image
#step_image = Step_image.find(params[:id])
end
def step_image_params
params.require(:step_image).permit(:text,:image1,:image2,:image3,:image4,:image5)
end
end
I did rake db:rollback STEP=1
$ rake db:rollback STEP=1
ActiveRecord::SchemaMigration Load (0.9ms) SELECT "schema_migrations".* FROM "schema_migrations"
ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateStepImages (20201119214001)
(0.1ms) BEGIN
== 20201119214001 CreateStepImages: reverting =================================
-- drop_table(:step_images)
(10.3ms) DROP TABLE "step_images"
(0.2ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: table "step_images" does not exist
: DROP TABLE "step_images"
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activesupport-4.2.11.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127:in `drop_table'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:665:in `block in method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:499:in `block in revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:606:in `exec_migration'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:590:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:768:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1023:in `block in execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `block in ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1022:in `execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:984:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:830:in `down'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:935:in `move'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:812:in `rollback'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:118:in `block (2 levels) in <top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `eval'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: table "step_images" does not exist
: DROP TABLE "step_images"
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activesupport-4.2.11.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127:in `drop_table'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:665:in `block in method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:499:in `block in revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:606:in `exec_migration'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:590:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:768:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1023:in `block in execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `block in ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1022:in `execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:984:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:830:in `down'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:935:in `move'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:812:in `rollback'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:118:in `block (2 levels) in <top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `eval'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `<main>'
Caused by:
PG::UndefinedTable: ERROR: table "step_images" does not exist
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127:in `drop_table'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:665:in `block in method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:499:in `block in revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:606:in `exec_migration'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:590:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:768:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1023:in `block in execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `block in ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1022:in `execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:984:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:830:in `down'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:935:in `move'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:812:in `rollback'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:118:in `block (2 levels) in <top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `eval'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)

When running rake db:migrate, rails will load the full application code before executing the migrations. Therefore, when you have already defined the StepImage class, rails will try to find the corresponding table. However, at this point in time, the table has not yet been created, which then causes this error. Try to comment out the StepImage class and everything associated with it (e.g. by moving the files out of the project folder) and re-run the migration. If it works, move the files back.
It seems like the rollback failed for the same reason: the step_images table has never been created. In such cases, I typically do the following:
Put the migration content into begin/rescue/end:
def change
begin
create_table :step_images do |t|
t.string :text
t.attachment :image1
#...
end
rescue
end
end
Rollback this migration to get to the original state. The rescue block will catch the error and the migration should succeed.
Use psql to check that the step_images table is actually gone. If not, delete it with psql.
Sanitize the source code: remove everything that has to do with the new step_images table. Start with this migration. Move it out of the project, so that rails c will not complain about a pending migration. Then move all models, controller, etc. out of the project. Remove all associations pointing to the new model (comment out). Otherwise, relations pointing to non-existing models/tables will cause more errors.
Sanitize until rails c succeeds in starting the application.
Move the migration back into db/migrations and run it.
If this succeeds, bring everything back in again. If unsure, do in multiple small steps and check in-between by running rails c.
Hope this helps!

Related

SQLite3::SQLException: in migration

Migration:
class AddUserIdToPhotos < ActiveRecord::Migration
def self.up
add_column :photos, :user_id, :integer
execute 'update photos inner join albums on albums.id = photos.album_id set photos.user_id = albums.user_id'
end
def self.down
remove_column :photos, :user_id
end
end
rake db:migrate, display error:
-- add_column(:photos, :user_id, :integer) -> 0.0010s
-- execute("update photos inner join albums on albums.id = photos.album_id set photos.user_id = albums.user_id") rake aborted!
StandardError: An error has occurred, this and all later migrations
canceled:
SQLite3::SQLException: near "inner": syntax error: update photos inner
join albums on albums.id = photos.album_id set photos.user_id =
albums.user_id/home/borowskiy/projects/galaxy/bdsmgalaxy/db/migrate/20110731191428_add_user_id_to_photos.rb:4:in
`up'
with trace:
SQLite3::SQLException: near "inner": syntax error: update photos inner join albums on albums.id = photos.album_id set photos.user_id = albums.user_id/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `new'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `prepare'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:134:in `execute'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `block in execute'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-3.2.22/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rack-mini-profiler-0.9.7/lib/patches/db/activerecord.rb:17:in `log_with_miniprofiler'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `execute'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:466:in `block in method_missing'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:438:in `block in say_with_time'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:438:in `say_with_time'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:458:in `method_missing'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:334:in `method_missing'
/home/borowskiy/projects/galaxy/bdsmgalaxy/db/migrate/20110731191428_add_user_id_to_photos.rb:4:in `up'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:370:in `up'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block in migrate'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:389:in `migrate'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:528:in `migrate'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `call'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/transactions.rb:208:in `transaction'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `ddl_transaction'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:719:in `block in migrate'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `each'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `migrate'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:570:in `up'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:551:in `migrate'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-3.2.22/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/home/borowskiy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/home/borowskiy/.rbenv/versions/2.0.0-p247/bin/rake:23:in `load'
/home/borowskiy/.rbenv/versions/2.0.0-p247/bin/rake:23:in `<main>'
Tasks: TOP => db:migrate
You SQL query is wrong. The correct sql update query should be like this
update photos set photos.user_id = albums.user_id from albums where albums.id = photos.album_id
Please change your migration file to this
class AddUserIdToPhotos < ActiveRecord::Migration
def self.up
add_column :photos, :user_id, :integer
execute "update photos set photos.user_id = albums.user_id from albums where albums.id = photos.album_id"
end
def self.down
remove_column :photos, :user_id
end
end
and run it again.

Rails - Mess with migrations

I'm hoping someone can help me sort this out without having to re-create my entire backend.
I'm using Rails and was having trouble deploying to Heroku. When I ran
$ heroku run rake db:migrate
my migration failed, because I had a migration that dropped a table that didn't exist. What had happened is that I had a table called Relations, and renamed it to SchoolCityType (singular).
This migration that dropped the table was dropping SchoolCityTypes (plural, as it should be).
Problem: I deleted the migration file instead of rolling it back, but Heroku still won't run the command.
What should I do? Thanks in advance.
heroku migration error message:
== 20160407014126 DropSchoolDistrictCityTypesTable: migrating =================
-- drop_table(:school_district_city_types)
(1.7ms) DROP TABLE "school_district_city_types"
PG::UndefinedTable: ERROR: table "school_district_city_types" does not exist
: DROP TABLE "school_district_city_types"
(1.1ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:`== 20160407014126 DropSchoolDistrictCityTypesTable: migrating =================
-- drop_table(:school_district_city_types)
(1.7ms) DROP TABLE "school_district_city_types"
PG::UndefinedTable: ERROR: table "school_district_city_types" does not exist
: DROP TABLE "school_district_city_types"
(1.1ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: table "school_district_city_types" does not exist
: DROP TABLE "school_district_city_types"
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/schema_statements.rb:116:in `drop_table'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:662:in `block in method_missing'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:632:in `block in say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:632:in `say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:652:in `method_missing'
/app/db/migrate/20160407014126_drop_school_district_city_types_table.rb:3:in `change'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:606:in `exec_migration'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:589:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:588:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:765:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:1041:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/transactions.rb:220:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:1041:in `ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:956:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:952:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:952:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:820:in `up'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:798:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: table "school_district_city_types" does not exist
: DROP TABLE "school_district_city_types"
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/schema_statements.rb:116:in `drop_table'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:662:in `block in method_missing'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:632:in `block in say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:632:in `say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:652:in `method_missing'
/app/db/migrate/20160407014126_drop_school_district_city_types_table.rb:3:in `change'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:606:in `exec_migration'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:589:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:588:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:765:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:1041:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/transactions.rb:220:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:1041:in `ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:956:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:952:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:952:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:820:in `up'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:798:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
PG::UndefinedTable: ERROR: table "school_district_city_types" does not exist
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/postgresql/schema_statements.rb:116:in `drop_table'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:662:in `block in method_missing'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:632:in `block in say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:632:in `say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:652:in `method_missing'
/app/db/migrate/20160407014126_drop_school_district_city_types_table.rb:3:in `change'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:606:in `exec_migration'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:589:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:588:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:765:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:1041:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/transactions.rb:220:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:1041:in `ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:956:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:952:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:952:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:820:in `up'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/migration.rb:798:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.2/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate`
you can still rollback even if you delete your migrations.
I suggest you rollback to a position you're comfortable with and work from there with new migrations.
Hope it helped
It's looks like you have deleted the migration file which creates 'school_district_city_types'. You are now trying to delete the table which does not exist/created before. Simply just delete the migration file '20160407014126' which drops that table.
You should be able to just delete the migration file. In the future write your migrations like this:
drop_table :school_district_city_types if (table_exists? :school_district_city_types)
and you will never have this issue again.

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.

Resources