【发布时间】:2021-02-16 08:32:54
【问题描述】:
在 Rails 6 上,我希望 rake db:setup 能够执行 db:create、db:migrate 和 db: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 在播种前没有进行必要的迁移
【问题讨论】:
标签: ruby-on-rails postgresql rake rails-migrations schema.rb