1

When migrating the database, I made a spelling mistake.

I want to generate a scaffold by running:

rails generate scaffold Micropost context:text user_id:integer
rails db:migrate

Although I made a mistake by leaving out the colon when I ran:

rails generate scaffold Micropost context:text user_id integer
rails db:migrate

I want to undo this migration, how to do it? (I'm using Rails 5.0.0.1)

When I run rails db:migrate, I get an error of:

 SQLite3::SQLException: table "microposts" already exists:

When I run rails db:migrate:status, I get the following output:

 Status   Migration ID    Migration Name  
 up       20161024021157  Create users  
 up       20161024025545  ********** NO FILE **********  
 down     20161024025805  Create microposts  

I tried to use rails db:migrate:down VERSION=20161024025805. There wasn't any message showing in the command line. Then I ran rails db:migrate. The error is the same.

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
niaomingjian
  • 3,472
  • 8
  • 43
  • 78

1 Answers1

5

rails db:rollback will simply rollback one migration which I believe is what you are looking for

For a more specific rollback, you can run rails db:migrate:down VERSION=numberofversion

Replace the numberofversion with the version number of the migration file generated, for example:

rails db:migrate:down VERSION=1843652238

Edit:

Since you are getting the error that the Microposts table already exists, you must follow these steps to remove the table:

  1. Run rails generate migration DropMicroposts
  2. Go to the /db/migrate folder and find the latest migration file you just created
  3. In that file paste this:

    class DropMicroposts < ActiveRecord::Migration 
      def up 
        drop_table :microposts 
      end 
    end 
    
  4. run rails db:migrate

  5. After this, run rails generate scaffold Micropost context:text user_id:integer
  6. Then run rails db:migrate
Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
  • The message of running `rails db:migrate:down VERSION=1843652238` tells me to use db:rollback. But running db:rollback shows nothing. It still doesn't work. – niaomingjian Oct 24 '16 at 04:06
  • Did you actually replace the version number with the version number of the migration you created? – Luka Kerr Oct 24 '16 at 04:07
  • Is Migration ID the version number ? – niaomingjian Oct 24 '16 at 05:04
  • In the db/migrate folder, you have files like 241294283_add_something_to_something . The version number is the number at the front of the file that you want to rollback – Luka Kerr Oct 24 '16 at 05:05
  • After doing `rails db:migrate:down VERSION=20161024025805`, it still says `ActiveRecord::StatementInvalid: SQLite3::SQLException: table "microposts" already exists` – niaomingjian Oct 24 '16 at 05:22
  • After running `rails db:migrate:down VERSION=20161024025805`, then I run `rails db:migrate`, it still says `ActiveRecord::StatementInvalid: SQLite3::SQLException: table "microposts" already exists` – niaomingjian Oct 24 '16 at 05:23
  • I think you need to read more on rails migrations. After rolling back a migration, you need to delete the migration file, then create a new migration that is *correct* and **then** run `rails db:migrate`. If you just rollback then migrate again, nothing has changed. – Luka Kerr Oct 24 '16 at 05:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126488/discussion-between-niaomingjian-and-luka-kerr). – niaomingjian Oct 24 '16 at 05:35