Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Generate a Scaffold

rake db:migrate - migrate fails due to 'create table'

Hi all,

I made an error when creating the scaffold and had to go back to amend it.

Now, when I try to migrate the database (whether generally, test or development), I get an error. This is because the migration is trying to create a table that already exists, todo_lists. The error looks like:

SQLite3::SQLException: table "todo_lists" already exists: CREATE TABLE "todo_lists" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar, "description" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)

Looking in the migration file, in the migration folder, I can see that the code does create a table.

migrate/date_number_create_todo_lists.rb
class CreateTodoLists < ActiveRecord::Migration
  def change
    create_table :todo_lists do |t|
      t.string :title
      t.text :description

      t.timestamps null: false
    end
  end
end

The schema looks similar:

schema.rb
ActiveRecord::Schema.define(version: 20150330151920) do

  create_table "todo_lists", force: :cascade do |t|
    t.string   "title"
    t.string   "string"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

end

In short, I don't really now what to do to get out of this so any help would be gratefully received.

Jason Seifer 's help would be great!

Trying to run the server gives an error when trying to access localhost:3000`, that says that the solution is to run:bin/rake db:migrate RAILS_ENV=development``` which is where I ended up above.

Cheers,

Steve.

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,437 Points

You normally shouldn't mess with migrations once they are generated, but let's make an exception.

1) Go through every single migration file you have in your db/migrate folder. Make sure that none of the code is duplicated. It is possible that one of the files creates the same table or column as some other file, which will return an error. 2) Go to console and do, in this order: rake db:drop, rake db:create and rake db:migrate. This will destroy the whole database and try to recreate it form your migration files. If this gives you the same errors, go back to point 1). 3) Read about migrations and how they work and how to use them

If the above doe snot help, please post a link to your github repo which contains the whole project.

Thanks Maciej,

My migrate folder has one migration, as shown above. I'll go ahead and drop, create and migrate. Do I need to generate the scaffold again, or is that set in stone now? That was what I initially did wrong and had to do again. That created an additional field in the DB table that still exists in the schema.rb but not in the migration file. The field is a string called string.

My Github is playing up too - I can't link my local git repo to Github for some reason. I've never had that problem before but I'm now getting some handshake error. fatal: unable to access 'https://github.com/OnlySteveH/': Server aborted the SSL handshake (128)

I'm looking in some forums to see if there's a solution to that too!

Today is not going well!

Thanks again,

Steve.

David Ker
David Ker
14,439 Points

Hey Steve, GitHub has been going through a DDOS attack for the past several days. I'd be willing to bet your SSL error is because of that. Keep trying and you should be able to get through eventually!

Maciej - perfect; thank you. That's now working.

David - thanks for the tip; I shall keep trying.

Steve.

Maciej Czuchnowski
Maciej Czuchnowski
36,437 Points

If you remove the column named string from the migration and recreate the database the way I told you, the schema should no longer have it. However your views and forms may still have it, so you have to manually change that.