rails destroy scaffold leaves back table

anonn023432 picture anonn023432 · Jan 31, 2014 · Viewed 8.1k times · Source

I created a new scaffold using the command:

rails generate scaffold level

But then I destroyed it using the command

rails destroy scaffold level

And then again added it back using the command

rails generate scaffold level question:string answer:string prev_q:integer next_q:integer

But now when I try rake db:migrate then I get the following error

 SQLite3::SQLException: table "levels" already exists: CREATE TABLE "levels" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "question" varchar(255), "answer" varchar(255), "prev_q" integer, "next_q" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)

My migrate/create_level.rb is

class CreateLevels < ActiveRecord::Migration
def change
  create_table :levels do |t|
  t.string :question
  t.string :answer
  t.integer :prev_q
  t.integer :next_q

  t.timestamps
  end
 end
end

But my schema.rb is:

  create_table "levels", :force => true do |t|
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

I want to know that how can I update the levels table in the schema. Also I would like to know that why doesn't the table get deleted when I destroy the scaffold. Do I need to run another command to do it?

Answer

Alex Siri picture Alex Siri · Jan 31, 2014

Using destroy scaffold does not run the rollback to the migration. The correct way of doing it would have been

rake db:rollback
rails destroy scaffold level

now, as you don't have that other migration anymore, you cannot roll it back. You'll need to delete that table manually:

rails dbconsole
DROP TABLE levels;