33

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
Harvey Katrina
  • 898
  • 1
  • 8
  • 14

4 Answers4

58

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.

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
  • Thanks so much! I don't know why they don't cover this in the guides. Neither does it explain how exactly you go about updating migrations correctly. When I update the migrate file and run `rake db:migrate` nothing happens. I know I'm doing something wrong, but I don't know what I should be doing. Do I have to give the migration a new name or something? – Nathan Dec 11 '13 at 01:51
  • 1
    Generally speaking, you shouldn't modify a migration file. If you really know what you're doing, you'll know when it's acceptable. If you have any doubts, don't modify; instead, make a new migration file (`rails generate migration`). There's one minor exception to this: if you want to modify the most-recent migration, you can do so, and then run `rake db:migrate:redo`, which will drop and rerun the last migration. This isn't always possible; it depends on the last migration being reversible (not all are). You shouldn't do this if you've already committed it to a shared repository. – Jim Stewart Dec 11 '13 at 05:39
  • The reason it did nothing is because Rails keeps track of which migrations have been run. It creates a table called `schema_migrations`, which contains a list of migration timestamps (the initial numeric part of a migration name). It won't re-run one that's already recorded there. That's how it knows which migrations to apply when you run `rake db:migrate`. – Jim Stewart Dec 11 '13 at 05:45
21

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

rake db:setup

Kannan S
  • 2,459
  • 1
  • 17
  • 17
  • Have been looking for that simple piece of information quite a bit... Was too trivial to find it easily it seems. It will generate the database from the models. – nembleton Feb 28 '16 at 12:37
2

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).

Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
0

For rails 6 & above, you can give this command to create a database, migrate all the migration files, and seed the data into the database:

rails db:prepare