29
votes

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
3
do i need to run db:create and db:migrate? or is this alreay setup once i edit the migrate file?Harvey Katrina
It means the table already exists. May be a previous migration already created it.rails_id

3 Answers

54
votes

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.

19
votes

We can simply give, it will do all the rake task which is require for database creation and migration

rake db:setup

1
votes

For Rails 5 and 6, the command is:

rails setup

This will "create the database, load the schema, and initialize it with the seed data" (docs).