0

Working on project in Rails 4.0.0, ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin10.8.0], with Rake 10.1.1

I was working on an app for a class in Rails, I made an error and deleted the changes made while in Git. When I went to redo the project, and ran the rake DB migration, I was given the following error message:

Joses-MacBook-Air:crumblr JRV$ rails generate migration CreateHearts Post_id:integer
  invoke  active_record
  create    db/migrate/20140120235500_create_hearts.rb
Joses-MacBook-Air:crumblr JRV$ bundle exec rake db:migrate
==  CreateHearts: migrating ===================================================
-- create_table(:hearts)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "hearts" already exists: CREATE TABLE "hearts" ("id" INTEGER        PRIMARY KEY AUTOINCREMENT NOT NULL, "Post_id" integer) /usr/local/rvm/gems/ruby-1.9.3-  p392/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
4.0.0/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top    (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

How do I get rid of the old table reference?

JRVazquez
  • 91
  • 2
  • 13
  • You're not being clear. By “old table reference”, do you mean you want to update your schema.rb to match what's in the existing database, or you want to get the database into the state that your codebase is expecting? Did you change the identifiers (timestamps) on your migrations? The database should be keeping track of which migration was last run. – coreyward Jan 21 '14 at 00:11

1 Answers1

4

There are a couple of commands you can use:

rake db:rollback will rollback the latest migration

rake db:rollback STEP=3 allows you to rollback by more than 1 migration (3 in this example)

rake db:reset will drop the database, recreate it and load the current schema into it.

and if you want to rollback then migrate back up again you can use

rake db:migrate:redo STEP=3 rollback 3 migrations and redo the migration

http://guides.rubyonrails.org/migrations.html

Aluxzi
  • 177
  • 1
  • 12
  • These helped, but the command that eventually worked was rake db:reset. Problem was it deleted my posts, but they were test items. – JRVazquez Jan 21 '14 at 01:17
  • There are other commands like `rake db:seed` runs the `db/seed.rb` file or `rake db:schema:load` to load the schema into the current database. See if this link helps explain further: [stackoverflow topic](http://stackoverflow.com/questions/10301794/difference-between-rake-dbmigrate-dbreset-and-dbschemaload) – Aluxzi Jan 21 '14 at 01:22