【问题标题】:rake db:migrate does not create tablerake db:migrate 不创建表
【发布时间】:2017-02-28 12:40:13
【问题描述】:

我正在使用 Rails 4.2.6 并且出现奇怪的错误(使用 HeadFirst 书籍、项目和目录名称 - “mebay”学习 rails):

我需要创建项目只使用“读取”的 CRUD - 所以我运行:

~/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

我的 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

(对我来说看起来还不错,基于早期的项目)

然后,据我了解,我需要创建数据库(我使用 sqlite):

~/mebay $ rake db:migrate

但在那之后,我的 development.sqlite3 仍然为空

我做错了什么?

【问题讨论】:

  • 在你执行rake db:migrate 之后,日志说了什么?
  • @araratan, ~/mebay $ rake db:migrate --trace ** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment ** Invoke db:load_config (first_time) ** Execute db:load_config ** Execute db:migrate ** Invoke db:_dump (first_time) ** Execute db:_dump ** Invoke db:schema:dump (first_time) ** Invoke environment ** Invoke db:load_config ** Execute db:schema:dump

标签: ruby-on-rails ruby sqlite rake


【解决方案1】:

其实你并没有做错什么。查看 db/schema.rb,如果你看到这个,那么你的数据库设置正确:

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

如果您收到表已存在的错误,您可以执行以下操作:

首先将down 方法添加到您用于生成广告表的迁移文件中。 down 方法如下所示:

def down
  drop_table :ads
end

然后运行rake db:migrate:down VERSION=version_number,其中version_number 是迁移文件名称中的时间戳。这将从数据库中删除该表。然后把change方法的名字改成up。保存文件并运行rake db:migrate:up 再次创建表。

【讨论】:

  • 是的,你是对的但是.. 但是书上说我需要用从他们的网站下载的数据库替换我的数据库(根据书 - 具有相同的结构)但替换后我得到 localhost:3000/ads/3Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development def check_pending!(connection = Base.connection) raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(connection) end def load_schema_if_pending!
  • 然后~/mebay $ rake db:migrate RAILS_ENV=development == 20161018144410 CreateAds: migrating ======================================== -- create_table(:ads) rake aborted! StandardError: An error has occurred, this and all later migrations canceled: SQLite3::SQLException: table "ads" already exists: CREATE TABLE "ads" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "description" text, "price" decimal, "seller_id" integer, "email" varchar, "img_url" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
  • 哦,在运行迁移之前,您需要先删除表。错误输出为SQLite3::SQLException: table "ads" already exists。要删除表,请运行此命令 rails generate migration drop_ads_table。然后打开那个新的迁移文件并将这个字符串复制到 def 方法 drop_table :ads 的主体中。保存文件,然后运行 ​​rake db:migrate
  • 咳咳.. :) 你的意思是 rake db:drop? (只是用谷歌搜索)
  • 咳咳.. :) 你的意思是 rake db:drop? (只是用谷歌搜索)~/mebay $ rake db:migrate RAILS_ENV=development == 20161018144410 CreateAds: migrating ======================================== -- create_table(:ads) -&gt; 0.0012s == 20161018144410 CreateAds: migrated (0.0013s) =============================== 正如我所料,删除了所有数据)ActiveRecord::RecordNotFound in AdsController#show Couldn't find Ad with 'id'=3 但如果它们具有相同的结构,我如何才能真正替换这些表?
【解决方案2】:

先运行rake db:create再运行rake db:migrate怎么样

【讨论】:

    猜你喜欢
    • 2020-01-26
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多