Question

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?

Was it helpful?

Solution 2

What I had to do was:

First I followed the steps specified by @Alex Siri.

But after doing that using

rake db:migrate

Did not add the values question,answer etc to the table, so I did the following steps

  1. I again deleted the scaffold using the command

    rails destroy scaffold level

  2. After that I again created the scaffold using the command:

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

  3. Finally I ran migration using

    rake db:migrate

This solved my problem. I wrote all the steps so that if anyone else has the same problem then they can easily solve it.

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top