crcerror
crcerror

Reputation: 119

rake db:migrate does not create table

I'm using Rails 4.2.6 and have strange error (learning rails with the HeadFirst book, project and directory name - "mebay"):

I need to create project uses only "read" of CRUD - so I run:

~/mebay $ rails generate model ad name:string description:text price:decimal seller_id:integer email:string img_url:string

Running via Spring preloader in process 8400
      invoke  active_record
   identical    db/migrate/20161018144410_create_ads.rb
   identical    app/models/ad.rb
      invoke    test_unit
   identical      test/models/ad_test.rb
   identical      test/fixtures/ads.yml

and here comes my db/migrate/20161018144410_create_ads.rb:

class CreateAds < ActiveRecord::Migration
  def change
    create_table :ads do |t|
      t.string :name
      t.text :description
      t.decimal :price
      t.integer :seller_id
      t.string :email
      t.string :img_url

      t.timestamps null: false
    end
  end
end

(looks pretty ok for me, basing on earlier projects)

Then, as I understand, I need to create database (i use sqlite):

~/mebay $ rake db:migrate

but after that, my development.sqlite3 remain empty

what am i doing wrong?

Upvotes: 1

Views: 7475

Answers (2)

Mark
Mark

Reputation: 11038

You actually didn't do anything wrong. Look in db/schema.rb and if you see this then you're database is setup properly:

ActiveRecord::Schema.define(version: 20161019035406) do

  create_table "ads", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
    t.decimal  "price"
    t.integer  "seller_id"
    t.string   "email"
    t.string   "img_url"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

end

If you're getting an error that the table already exists, you can do the following:

first add a down method to your migration file that you used to generate the ads table. The down method would look like this:

def down
  drop_table :ads
end

Then run rake db:migrate:down VERSION=version_number where the version_number is the timestamp in the name of the migration file. This will remove the table from the database. Then change the name of the change method to up. Save the file and run rake db:migrate:up to create the table again.

Upvotes: 3

Yu-Shing Shen
Yu-Shing Shen

Reputation: 372

How about run rake db:create first and then run rake db:migrate

Upvotes: 0

Related Questions