簡體   English   中英

Rake db:setup 不運行 rails db:migrate,schema.rb 不存在錯誤

[英]Rake db:setup does not run rails db:migrate, schema.rb does not exist error

在 Rails 6 上,我希望rake db:setup執行db:createdb:migratedb:seed
我只有一個數據模型,其表的記錄創建是在db/seeds.rb

# db/seeds.rb
require 'csv'

csv_text = File.read(Rails.root.join('lib', 'seeds', 'seed_first_table.csv'))
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1')
csv.each_with_index do |row, index|
  cr = CatProfile.new
  puts "row #{index} saved"
end

puts "There are now #{CatProfile.count} rows in the table"

database.yml還有一個新的用戶憑證,它是通過sudo -u postgres createuser -s new_user

但是rake db:setup返回:

Created database 'my_application_development'
Created database 'my_application_test'
my_application/db/schema.rb doesn't exist yet. Run `rails db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /my_application/config/application.rb to limit the frameworks that will be loaded.

sudo -u postgres psql它確實創建了數據庫,但沒有關系(沒有種子表)

當我運行rails db:migrate

== 20201103153526 CreateCatProfiles: migrating =================================
-- create_table(:cat_profiles)
   -> 0.0091s
== 20201103153526 CreateCatProfiles: migrated (0.0092s) ========================

然后當我第二次運行rake db:setup時:

Database 'my_application_development' already exists
Database 'my_application_test' already exists
row 0 saved
row 1 saved 
...
row 1719 saved
There are now 1720 rows in the table

注意到數據庫已經從第一個rake db:setup存在,但現在它能夠為表做種子

我不明白為什么第一次運行rake db:setup沒有在播種前進行必要的遷移

如果您查看rails db:setup源代碼,您將看到db:setup不運行db:migrate

創建數據庫,加載模式,並用種子數據初始化(使用 db:reset 也首先刪除數據庫)

db:schema:loadschema.rb當前存在的內容加載到數據庫中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM