Rails 的多个数据库不适用于远程数据库

编程入门 行业动态 更新时间:2024-10-24 22:30:37
本文介绍了Rails 的多个数据库不适用于远程数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个远程只读 postgres 数据库,它由 cardano-db-sync.

I have a remote read-only postgres database that is maintained from a docker instance of cardano-db-sync.

我设法将开发数据库连接到它,它工作正常.但由于它是只读的,我想为用户和其他可修改表添加另一个数据库.

I managed to connect the development database to it, it was working fine. But since it is read-only I wanted to add another database for User and other modifiable tables.

这是我准备的设置:

# 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'] %>

然后是文档说明

# 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

然后是一些要测试的表:

then some tables to test:

# 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,我就会收到此错误:

The primary database seems to work fine with rails c but once using PoolHash I get this error:

$ 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

I don't get why it says cexplorer is not configured for development when is instead right there in database.yml in the same config that worked when it was without the :primary

更新

似乎通过移动 <<: *default 事情有所改善:

it seems that by moving the <<: *default things improved:

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'] %>

但我现在遇到了一个不同的问题:

but I now get a different problem:

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 年.

I have now tried a setup as in this article managing multiple databases in a single rails application link from 2017.

它工作正常.与 Rails 文档中的内容相比,它对我的​​目标也更有意义.我希望作为一篇旧文章不会给我带来惊喜.

It works fine. It also makes more sense for my goal than what is in the rails documentation. I hope being an old article won't give me surprises down the line.

推荐答案

我遇到了同样的问题,我的解决方案是将 establish_connection·:cexplorer 放在下面的 CexplorerRecord 类中connects_to 行.

I had the same issue my solution was to put establish_connection·:cexplorer in my CexplorerRecord class below the connects_to line.

更多推荐

Rails 的多个数据库不适用于远程数据库

本文发布于:2023-07-08 20:17:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   多个   不适用于   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!