Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

rake db:migrate - rake aborted bc SQLite3 table already exists?

I'm trying to do the first db:migrate steps in the todo list app track but I keep getting a "rake aborted!" error message. Here is the full error message:

railsapi:~/projects/odot (master *) $ rake db:migrate
==  CreateTodoLists: migrating ================================================
-- create_table(:todo_lists)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "todo_lists" already exists: CREATE TABLE "todo_lists" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "description" text, "created_at" datetime, "updated_at" datetime) /home/treehouse/projects/odot/db/migrate/20150402161626_create_todo_lists.rb:3:in `change'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "todo_lists" already exists: CREATE TABLE "todo_lists" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "description" text, "created_at" datetime, "updated_at" datetime) 
/home/treehouse/projects/odot/db/migrate/20150402161626_create_todo_lists.rb:3:in `change'
SQLite3::SQLException: table "todo_lists" already exists
/home/treehouse/projects/odot/db/migrate/20150402161626_create_todo_lists.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Any help would be huge!

1 Answer

Looks like, as the error is stating, the table "todo_lists" already exists and a migration is trying to create it. Make sure you don't have multiple migrations that try to create that table. If you're trying to add fields to it make sure you use a change method in your migration and call add_column or one of the related methods to make changes (in other words don't try to re-create the table to make changes).

It could also be that your migration versions have gotten mixed up and it's just trying to re-run the previous migration that creates that table. To test that you can drop dropping the database and starting over with your migrations. This isn't ideal and is not something you would want to do with production-level data but in development you can always do that with the following:

rake db:drop db:create db:migrate

If that completes successfully then you should be good to go. Hope that helps!

how come rollback doesn't delete the table created?

also when would you use each of these?

  1. db:schema:load creates tables and columns within the (existing) database following schema.rb
  2. db:setup does db:create, db:schema:load, db:seed
  3. db:reset does db:drop, db:setup