gpt4 book ai didi

ruby - Heroku:数据库配置没有指定适配器

转载 作者:数据小太阳 更新时间:2023-10-29 08:00:26 24 4
gpt4 key购买 nike

我在 heroku 中部署了一个应用程序,它使用 postgresql 作为数据库。我创建了一个新模型,它将连接到远程 mysql 数据库,如下所示:

# other_database.rb(apps/models/)
class OtherDatabase < ActiveRecord::Base
self.abstract_class = true
establish_connection "remote_#{Rails.env}"
end

# other_model.rb(apps/models/)
class OtherModel < OtherDatabase
self.table_name = "users"
end

我还编辑了 database.yml 文件并添加了这些条目:

remote_development:
adapter: mysql2
encoding: utf8
reconnect: false
database: app_development
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock

remote_production:
adapter: mysql2
encoding: utf8
reconnect: true
database: app_production
pool: 40
username: root
password: secretpassword
socket: /var/run/mysqld/mysqld.sock
host: 12.34.56.78

远程数据库使用mysql作为数据库。

它在我的本地机器上工作正常,但是当我将它部署到 heroku 时,它创建了一个错误页面,所以我查看了 heroku 日志,我发现了这个:

/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/connection_specification.rb:52:in `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/connection_specification.rb:46:in `resolve_string_connection'

我几乎所有的时间都在寻找一些答案,但无济于事。希望任何人都可以帮助解决这个问题。

TIA。

埃拉夫

最佳答案

Heroku 删除并重新创建 database.yml 文件。这意味着您在 database.yml 文件中放入的任何内容都将被 heroku 完全忽略。

像这样在您的 establish_connection 方法调用中弹出您的数据库凭据:

establish_connection(
adapter: 'mysql2',
encoding: 'utf8',
reconnect: true,
database: 'app_production',
pool: 40,
username: 'root',
password: 'secretpassword',
socket: '/var/run/mysqld/mysqld.sock',
host: '12.34.56.78'
)

但是最好使用数据库 URL 并将其存储在 heroku 的环境变量中。

heroku config:set MYSQL_DB_URL=http://example.com/rest_of_the_url

然后使用

establish_connection(ENV['MYSQL_DB_URL'])

可在此处找到 URL 的示例和文档:http://forums.mysql.com/read.php?39,10734,10750

关于ruby - Heroku:数据库配置没有指定适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17230994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com