0

I am currently working on a project and I have messed up my code. I went back and deleted the git branch. I am now going back through and adding all of the code back.

I need to create a db migration but the migration that I need to create says it already exists. I don't see how this could be possible.

My question is how would I get rails to just overwrite the table?

1 Answers1

0

Although you deleted your git branch and local code, if you didn't delete your database, it remains the same so your table still exists.

You can create a migration to drop a table. Code from this answer.

rails generate migration DropProductsTable

class DropProductsTable < ActiveRecord::Migration
  def up
    drop_table :products
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end

I don't know what database you are using, but if the table is the same as what you want to add you may not even need to drop it.

Community
  • 1
  • 1
Rob Hughes
  • 876
  • 2
  • 13
  • 32
  • I see. I am currently working through a bootcamp course and I messed up the branch so I deleted it. The database I am using is SQLite3. So if the data for the table is the same as the first one I can just leave it? – Jonathan Reeves Feb 23 '17 at 20:38
  • This was my out put redhoodjt:~/workspace/saasapp (contact_form) $ rails db:migrate == 20170223181245 CreateContacts: migrating =================================== -- create_table(:contacts) rails aborted! StandardError: An error has occurred, this and all later migrations canceled: – Jonathan Reeves Feb 23 '17 at 21:00
  • Yes. Your database hasn't been touched even though you deleted the branch so the table is still there. You could also use ` rake db:rollback` to undo the last migration you made, or `rake db:rollback STEP=n` (n being the number of migrations to rollback) depending on when you created this table. Then `rake db:migrate` with your new migrations that you've created. – Rob Hughes Feb 23 '17 at 21:24
  • The only problem with that is that for some reason the error that I am being given says that all later migrations canceled. – Jonathan Reeves Feb 23 '17 at 21:30
  • Yes. If you have 3 migration files and the first one gives you the error because the table is already there, the 2 that come after it will get cancelled. You need to resolve the issue with the problem migration by dropping the table/rolling back or removing the migration file. Is the original migration that you made for this table still there, or did you delete it? – Rob Hughes Feb 23 '17 at 21:35
  • I deleted it. At least I think that I did. – Jonathan Reeves Feb 23 '17 at 21:43
  • Ok I deleted it and that fixed the issue. Thank you. – Jonathan Reeves Feb 23 '17 at 23:19
  • But now I have another issue. When I try to see if my submission form works I get an error saying: No route matches [POST]"/contacts/new" When I go to my routes.rb file though I have resources :contacts in there. – Jonathan Reeves Feb 23 '17 at 23:20
  • Without seeing your code it is impossible to give you an answer for that. – Rob Hughes Feb 24 '17 at 12:56
  • Ok. I can post it. How do I post code so it shows up like code? And from what file or files do you need the code from? – Jonathan Reeves Feb 24 '17 at 14:33
  • Lets continue this discussion in [chat](http://chat.stackoverflow.com/rooms/136554/no-route-matches-post) – Rob Hughes Feb 24 '17 at 14:47
  • My reputation isn't high enough to chat. – Jonathan Reeves Feb 24 '17 at 14:49
  • Ok that's a pain. You should post this as a new question then, add the form code from your view, routes, and new and create controller methods – Rob Hughes Feb 24 '17 at 14:51