ruby-on-rails - rake db:migrate不创建表

标签 ruby-on-rails ruby sqlite rake

我正在使用Rails 4.2.6,并遇到奇怪的错误(使用HeadFirst的书,项目和目录名称-“ mebay”学习Rails):

我只需要使用CRUD的“ read”来创建项目,所以我运行:

~/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仍然为空

我究竟做错了什么?

最佳答案

您实际上没有做错任何事情。查看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方法添加到用于生成ads表的迁移文件中。 down方法如下所示:

def down
  drop_table :ads
end


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

关于ruby-on-rails - rake db:migrate不创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40121805/

相关文章:

ruby-on-rails - 如何撤消 Rails 中最近的迁移

css - Rails 4 应用程序中带有图像标签的全宽响应图像

ruby - 在嵌套函数中移交变量的适当解决方案(在 ruby​​ 中)

ruby-on-rails - 如何在rails上实现文本提取?

node.js - nodejs electronjs sqlite3 - 使用未声明的标识符 'napi_is_detached_arraybuffer'

javascript - 简单模式弹出窗口?

ruby-on-rails - 为什么不能给这个继承的类属性赋值呢?

ruby - Ruby 类什么时候可以有多个父类(super class)

c++ - sqlite中的嵌套语句

python - Tkinter Treeview 列排序