new to rails, setting up db then running rake db:create/migrate

Harvey Katrina picture Harvey Katrina · May 26, 2013 · Viewed 70.8k times · Source

hi im currently learning rails, and following a tutorial. the instructions were to edit the migration file after i've created the app, then running rake db:migrate, then rake db:create.

i've edited the migration file to this:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :username
      t.string :email
      t.string :encrypted_password
      t.string :salt
      t.timestamps
    end
  end
end

then when i've run 'rake db:migrate' i got an error

Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` ...

after i'm supposed to run 'rake db:create', then im getting this

user_auth_development already exists
user_auth_test already exists

Answer

Jim Stewart picture Jim Stewart · May 26, 2013

You run rake db:create once and only once, and you run it first. Then you run rake db:migrate every time you add/change a migration. You've either already run this migration, or you are pointing at a database that already exists and already contains a table named users. My guess is that you ran the migration once already, in which case you're probably good to go. If you want to nuke the DB and start over, do rake db:drop db:create db:migrate.