在我的 Rails 应用程序上 运行 "heroku run rake db:migrate" 时出现 YAML 语法错误

YAML syntax error when running "heroku run rake db:migrate" on my Rails app

我已经搜索了这个问题的每个解决方案几个小时,但我就是无法让它工作。

我有一个 Rails 应用程序,我正在尝试将其部署到 heroku,但是当我 运行 heroku run rake db:migrate 时,我收到此错误:

rake aborted!
YAML syntax error occurred while parsing /app/config/database.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Error: (<unknown>): did not find expected key while parsing a block mapping at line 7 column 3

我已经使用 YAML validator 来验证我的 database.yml,但它仍然不起作用。这是它的样子:

# database.yml
--- 
default: 
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development: 
  adapter: postgresql
  database: chamada_development
  encoding: unicode
  password: "<%= ENV['CHAMADA_DATABASE_PASSWORD'] %>"
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: chamada
production: 
  adapter: postgresql
  database: chamada_production
  encoding: unicode
  password: "<%= ENV['CHAMADA_DATABASE_PASSWORD'] %>"
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: chamada
test: 
  adapter: postgresql
  database: chamada_test
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

我该如何解决这个问题?我不知道。

在行中使用单引号

  password: <%= ENV['CHAMADA_DATABASE_PASSWORD'] %>

你使用了双引号,所以它就像:

"<%= ENV[" CHAMADA_DATABASE_PASSWORD "] %>"

这就是错误的原因。

更新:我有一段时间没有在 Heroku 上工作了,但是我在我的 database.yml 文件中发现了一个包含这些注释的旧项目:

# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>

您可能需要确保正确设置了环境变量。通过 运行 heroku config --app <your-app-name>

检查

你应该不需要 ----

如果您要设置默认值,您不妨使用它来避免重复自己。并且还使用单引号并删除字符串插值。您可以在每个组之间添加空行。

default: &default 
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch('RAILS_MAX_THREADS') { 5 } %>

development:
  <<: *default 
  database: chamada_development
  password: <%= ENV['CHAMADA_DATABASE_PASSWORD'] %>
  username: chamada

production: 
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

test:
  <<: *default
  adapter: postgresql
  database: chamada_test

我刚刚解决了这个问题,从头开始逐步在 Heroku 中部署应用程序。