gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:31:46 25 4
gpt4 key购买 nike

我正在使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com