【问题标题】:Rake db:setup does not run rails db:migrate, schema.rb does not exist errorRake db:setup 不运行 rails db:migrate, schema.rb 不存在错误
【发布时间】:2021-02-16 08:32:54
【问题描述】:

在 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 在播种前没有进行必要的迁移

【问题讨论】:

    标签: ruby-on-rails postgresql rake rails-migrations schema.rb


    【解决方案1】:

    如果您查看source coderails db:setup,您将看到db:setup 不会运行db:migrate

    创建数据库,加载架构,并使用种子数据进行初始化(使用 db:reset 也可以先删除数据库)

    db:schema:load 将您的schema.rb 中当前存在的内容加载到数据库中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 2011-07-14
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      相关资源
      最近更新 更多