Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake db:migrate is not working

Tags:

I'm working through the rails tutorial and have gotten stuck. Starting at Listing 8.16 I have made the following modifications to <timestamp>_add_remember_token_to_users.rb:

class AddRememberTokenToUsers < ActiveRecord::Migration   def change     add_column :users, :remember_token, :string     add_index  :users, :remember_token   end end 

The guide then says to update dev & test db as usual:

$ bundle exec rake db:migrate $ bundle exec rake db:test:prepare 

My User test for the *remember_token* is still failing so I took a look at the user table in dev and tests database with command line sqlite3. They look like this:

sqlite> .schema users CREATE TABLE "users" (    "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,     "name" varchar(255),     "email" varchar(255),     "created_at" datetime NOT NULL,     "updated_at" datetime NOT NULL,     "password_digest" varchar(255)); CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email"); 

It seems like my migration has not been run yet but I do not know how to force it to run.

like image 837
hippeelee Avatar asked Mar 27 '12 19:03

hippeelee


People also ask

Which file will be modified during rails migration?

Migrations give you a way to modify your database schema within your Rails application. So you use Ruby code instead of SQL. Using Rails migrations instead of SQL has several advantages. Rails applications that can stay within the Active Record model are database-independent.

How does rake db migrate work?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.


2 Answers

Try to rebuild your database structure(WARNING: all db-data will be lost):

rake db:drop:all rake db:create:all rake db:migrate 

If you use Rails < 4.1, don't forget to prepare test database:

rake db:test:prepare 

This is the easiest solution since you are working with tutorial. However in production or having important data in development you should take time to investigate the issue. In this case you most likely had created an empty migration, ran rake db:migrate, then added instructions to the migration, so you don't see a new field and further rake db:migrate does nothing. To resolve this issue you need to comment your change instructions, perform rake db:rollback, uncomment instructions and then rake db:migrate to apply instructions you missed.

like image 137
Rustam A. Gasanov Avatar answered Oct 13 '22 19:10

Rustam A. Gasanov


I had the same issue as the initial question. $ bundle exec rake db:migrate wasn't adding remember_token to the .db and Latha Doddikadi's answer worked for me.

I did:

rake db:rollback 

and then:

$ bundle exec rake db:migrate 

which added the remember_token field to the database followed by:

bundle exec rspec spec/models/user_spec.rb 

which passed.

Finished in 0.92841 seconds 21 examples, 0 failures 
like image 37
Livi17 Avatar answered Oct 13 '22 20:10

Livi17