1

A few days ago I created a migration for rails to add Paperclip avatars, but ended up going a different direction. Being new to Rails, I didn't know how bad of an idea it was to delete the migration file like I did.

My app works fine locally, but when run heroku run rake db:migrate I get this:

undefined method `attachment' for #<ActiveRecord::ConnectionAdapters::Table:0x000000046092e0>

It is because it is trying to run a migration called AddAttachmentAvatarToVenues, which is the migration I stupidly deleted.

It was also adding columns for the avatars that were specified in the deleted migration to the schema.rb, but I created a new migration to get rid of these. The new migration got rid of them but didn't change the heroku migration error.

Any idea how to fix this? I've done lots of googling and looking around SO and while there a lot of people with similar errors they are mostly problems with the commands that they're using.

Here is the output after the deleted migration attempted in my heroku migration.

==  AddAttachmentAvatarToVenues: migrating ====================================
-- change_table(:venues)
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `attachment' for #<ActiveRecord::ConnectionAdapters::Table:0x00000003bdb7c8>
/app/db/migrate/20130206222434_add_attachment_avatar_to_venues.rb:4:in `block in up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/schema_statements.rb:243:in `change_table'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:466:in `block in method_missing'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:438:in `block in say_with_time'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:438:in `say_with_time'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:458:in `method_missing'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:334:in `method_missing'
/app/db/migrate/20130206222434_add_attachment_avatar_to_venues.rb:3:in `up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:370:in `up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:410:in `block in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:389:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:528:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `call'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `ddl_transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:719:in `block in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `each'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:570:in `up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:551:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

I do see what's wrong with the output, I'm just not sure how to fix it without messing it up more.

EDIT: Here's some screenshots of file structure:

git

(The two similarly named ones are because there was a column I forgot to remove, but this was after I was having this problem and didn't affect it either way)

structure

EDIT2:

Here's the deleted migration from my git history. I had added some more channels after this. They were just a couple of strings, but if that could make a difference I'll find a newer version.

class AddAttachmentAvatarToVenues < ActiveRecord::Migration
  def self.up
    change_table :venues do |t|
      t.attachment :avatar
    end
  end

  def self.down
    drop_attached_file :venues, :avatar
  end
end

Thanks in advance!

Jason
  • 3,330
  • 1
  • 33
  • 38
  • can you show the migration? – Nick Ginanto Feb 11 '13 at 18:31
  • That's the problem, I deleted it from my migrations folder and I'm not sure where else it would be coming from. – Jason Feb 11 '13 at 18:39
  • its not here? /app/db/migrate/20130206222434_add_attachment_avatar_to_venues.rb – Nick Ginanto Feb 11 '13 at 18:40
  • No it doesn't seem to be in the migrate folder locally or in the git files I'm pushing. BUT it's weird because my migrate folder isn't in /app/ anyways, is there a config or setting I'm missing? I edited my question with a couple screenshots, hopefully that helps. Thanks so much for helping me out! – Jason Feb 11 '13 at 18:52
  • what happens if you do `rake db:rollback` – Nick Ginanto Feb 11 '13 at 18:57
  • You don't have the migration in your git commit history? – Nick Feb 11 '13 at 20:36
  • It's in my git commit history, I was just saying that there wasn't a migration in my current master branch that wasn't in my local version. Edited question with the migration from my history. – Jason Feb 11 '13 at 22:22

1 Answers1

2

Perhaps you should look into this: How to empty DB in heroku

All your normal commands are also available in heroku, the only difference is that you have to put heroku run in front of it.

If your application hasn't gone live yet you could simply reset the database:

heroku pg:reset SHARED_DATABASE --confirm NAME_OF_THE_APP

And recreate it, using:

heroku run rake db:migrate

To seed the database:

heroku run rake db:seed

And lastly, to restart Heroku:

heroku restart

P.S. If these steps don't help, you could try running "heroku run rake db:setup" after dropping the database

Community
  • 1
  • 1
Peter de Ridder
  • 2,379
  • 19
  • 18
  • Thanks for the help, unfortunately when I did this the `AddAttachmentAvatarToVenues: migrating` still tried and aborted, causing the rest of my migrations to abort as well just like before. – Jason Feb 11 '13 at 19:04
  • You could try: heroku run rake db:rollback. So your migrations are rolled back to the start. Then you could create the migration AddAttachmentAvatarToVenues with the desired attributes. Finally, you can run heroku run rake db:migrate again. – Peter de Ridder Feb 11 '13 at 19:28
  • Alternative: git rm [migration_number]_create_users.rb git push heroku master heroku run rake db:drop => heroku run rake db:create => heroku run rake db:migrate, see: http://stackoverflow.com/questions/11023645/heroku-run-rake-dbmigrate-error – Peter de Ridder Feb 11 '13 at 20:40