【问题标题】:`heroku run rake db:seed` fails without error; tables are present but empty in remote db`heroku run rake db:seed` 失败且没有错误;表在远程数据库中存在但为空
【发布时间】:2020-08-14 08:07:59
【问题描述】:

我已经阅读了人们在使用此命令时遇到的许多错误,但我的数据库未能在没有任何明显错误的情况下进行种子设定。

我已经为 Heroku 部署了一个 Rails 后端。 postgres 迁移成功,我可以使用heroku pg:psql 查看表,但运行heroku run rake db:seed 后,它们仍然为空:

myappname::DATABASE=> SELECT * FROM cities;
id | name | country | lat | long | population | created_at | updated_at 
----+------+---------+-----+------+------------+------------+------------
(0 rows)

我的文件夹结构是:

root
|- app
  |- db
    |- migrate
      |- [list of migrations].rb
    |- schema.rb
    |- seeds.rb

seeds.rb 不在.gitignore 中。文件示例如下所示:

City.destroy_all
Game.destroy_all
Question.destroy_all

c001 = City.create(name: 'Tokyo', country: 'Japan', lat: 35.685, long: 139.7514, population: 35676000)
c002 = City.create(name: 'New York', country: 'United States', lat: 40.6943, long: -73.9249, population: 19354922)
c003 = City.create(name: 'Mexico City', country: 'Mexico', lat: 19.4424, long: -99.131, population: 19028000)
c004 = City.create(name: 'Mumbai', country: 'India', lat: 19.017, long: 72.857, population: 18978000)

我正在使用ruby 2.6.1rails 5.2.3pg 1.1.4

如果有任何关于尝试的建议,我们将不胜感激!

【问题讨论】:

  • 您在种子中使用了 safe 方法。这意味着如果某事未能完成,它不会抛出错误。执行将悄悄地继续。这可能是即使失败也看不到任何输出的原因。尝试将destroy_all 替换为all.each(&:destroy!)delete_all,将create 替换为create!。那么如果有什么失败了,至少你会知道。
  • @SiimLiiser 谢谢——这是非常有用的信息。今天早上,我用新的眼光发现#comment 之前的文件中出现了一个杂散词,可能是我的蓝牙键盘出现故障时。

标签: ruby-on-rails heroku rake heroku-postgres seed


【解决方案1】:

问题解决了。这是一个错字,但我会把它留在这里,以防有人做类似的事情并且没有错误失败。我的文件顶部是这样的:

exit# TODO: I had a todo item here...
# TODO: And another
# TODO: There were three comments

City.destroy_all
Game.destroy_all
Question.destroy_all

所以在本地运行和部署到 Heroku 之间,我一定是在我认为我的光标在控制台中时输入了那个“退出”,而在 260 行文件中没有注意到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2011-02-08
    • 1970-01-01
    • 2015-07-11
    • 2019-10-05
    • 2014-11-17
    相关资源
    最近更新 更多