Search code examples
ruby-on-railsrubydbmigrate

How does rake db:migrate VERSION=0 work?


I'm new to ruby. I got this error

 bundle exec rake db:migrate

== 20150423205259 AddActivationToUsers: migrating =============================
-- add_column(:users, :activation_digest, :string)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: activation_digest: ALTER TABLE "users" ADD "activation_digest" varchar/home/myusername/.rvm/gems/ruby-2.2.0/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in `initialize'

and it was solved when I run rake db:migrate VERSION=0 If I really have duplicate column name how does rake db:migrate VERSION=0 solve it? And if not why did I get that error?


Solution

  • It basically runs the very first migration. You can specify any version number you want to migrate your database to.

    You probably did "rails generate model ...." twice on the same model, but didn't destroy one of those migrations, it still exists in the "db/migrate/" folder.

    I suggest you read a bit about migrations here.