Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rake db:migrate error, table already exists

I accidentally create a migration which I did not need so I delete the file, and created a new migration now when I try to run rake db:migrate I keep getting this error. I am the using SQlite3 gem, and Ruby on Rails 4

StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "categories" already exists: CREATE TABLE "categories" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "category_name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NO
T NULL) D:/muse/db/migrate/20150830113519_create_categories.rb:3:in `change'
D:in `migrate'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "categories" already exists: CREATE TABLE "categories" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "category_name" varchar, "created_at" datetime NO
T NULL, "updated_at" datetime NOT NULL)
D:/muse/db/migrate/20150830113519_create_categories.rb:3:in `change'
D:in `migrate'
SQLite3::SQLException: table "categories" already exists
D:/muse/db/migrate/20150830113519_create_categories.rb:3:in `change'
D:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
like image 406
user3914956 Avatar asked Dec 20 '22 00:12

user3914956


2 Answers

This is ocurried with me because I'm trying to do some samples in diferents branchs. So I do this and works:

rake db:drop
rake db:reset or rake db:setup
like image 187
rld Avatar answered Jan 18 '23 21:01

rld


When you create a migration and run it, and want to make chances, you must first rollback:

$ bin/rake db:rollback STEP=1

At this stage you can delete the file or modify it and run again the migration_

$ bin/rake db:migrate

Now that you deleted the file, you cant rollback. What you have to do is delete the table manually or create a migration to drop that table:

$ bin/rails generate migration DropCategoriesTable

This will generate a migration file. Edit it:

class DropCategoriesTable < ActiveRecord::Migration
  def up
    drop_table :categories
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end
like image 36
WeezHard Avatar answered Jan 18 '23 21:01

WeezHard