gpt4 book ai didi

ruby-on-rails - Rails 的多个数据库不适用于远程数据库

转载 作者:行者123 更新时间:2023-12-04 13:54:12 26 4
gpt4 key购买 nike

我有一个远程只读 postgres 数据库,它由 cardano-db-sync 的 docker 实例维护。 .
我设法将开发数据库连接到它,它工作正常。但由于它是只读的,我想为用户和其他可修改表添加另一个数据库。
这是我准备的设置:

# config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 5

development:
<<: *default
primary:
database: 'db_sync'
cexplorer:
database: 'cexplorer'
username: <%= ENV['HOST_PG_DATABASE_USERNAME'] %>
password: <%= ENV['HOST_PG_DATABASE_PASSWORD'] %>
host: <%= ENV['HOST_PG_DATABASE_IP'] %>
port: <%= ENV['HOST_PG_PORT'] %>
然后是抽象类,如 doc instructions
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

connects_to database: { reading: :primary, write: :primary}
end

# app/models/cexplorer_record.rb
class CexplorerRecord < ApplicationRecord
self.abstract_class = true

connects_to database: { reading: :cexplorer }
end
然后是一些要测试的表:
# app/models/pool_hash.rb
class PoolHash < CexplorerRecord
self.table_name = 'pool_hash'
end

# app/models/pool.rb
class Pool < ApplicationRecord
end
主数据库似乎与 rails c 一起工作正常但是一旦使用 PoolHash 我得到这个错误:
$ rails c
Running via Spring preloader in process 79267
Loading development environment (Rails 6.0.3.4)
2.6.1 :001 > PoolHash.count
Traceback (most recent call last):
4: from (irb):1
3: from app/models/pool_hash.rb:1:in `<main>'
2: from app/models/cexplorer_record.rb:1:in `<main>'
1: from app/models/cexplorer_record.rb:4:in `<class:CexplorerRecord>'
ActiveRecord::AdapterNotSpecified (The `cexplorer` database is not configured for the `development` environment.)

Available databases configurations are:

default
development
2.6.1 :002 > Pool.count
(0.5ms) SELECT COUNT(*) FROM "pools"
=> 0
2.6.1 :003 >
我不明白为什么它说 cexplorer 没有配置用于开发,而是在 database.yml 中。在没有 :primary 时工作的相同配置中

更新
似乎通过移动 <<: *default事情有所改善:
development:
primary:
<<: *default
database: 'swan_db_sync'
cexplorer:
<<: *default
database: 'cexplorer'
username: <%= ENV['HOST_PG_DATABASE_USERNAME'] %>
password: <%= ENV['HOST_PG_DATABASE_PASSWORD'] %>
host: <%= ENV['HOST_PG_DATABASE_IP'] %>
port: <%= ENV['HOST_PG_PORT'] %>
但我现在遇到了一个不同的问题:
rails c        
Running via Spring preloader in process 79798
Loading development environment (Rails 6.0.3.4)
2.6.1 :001 > Pool.count
(4.2ms) SELECT COUNT(*) FROM "pools"
=> 0
2.6.1 :002 > PoolHash.count
Traceback (most recent call last):
1: from (irb):2
ActiveRecord::ConnectionNotEstablished (No connection pool with 'CexplorerRecord' found.)

更新 2
我现在尝试了一种设置,如本文中在单个 Rails 应用程序中管理多个数据库 link从 2017 年开始。
它工作正常。与 Rails 文档中的内容相比,它对我的​​目标也更有意义。
我希望作为一篇旧文章不会给我带来惊喜。

最佳答案

我遇到了同样的问题,我的解决方案是放置 establish_connection·:cexplorer在我的 CexplorerRecord类低于 connects_to线。

关于ruby-on-rails - Rails 的多个数据库不适用于远程数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65541744/

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