postgresql Rails 中的多数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1298909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:47:56  来源:igfitidea点击:

Multiple database connection in Rails

mysqlruby-on-railspostgresqlmultiple-databases

提问by Hoang Tran

I'm using active_delegate for multiple connection in Rails. Here I'm using mysql as master_database for some models,and postgresql for some other models.

我在 Rails 中使用 active_delegate 进行多个连接。在这里,我使用 mysql 作为某些模型的 master_database,以及其他模型的 postgresql。

Problem is that when I try to access the mysql models, I'm getting the error below! Stack trace shows that, it is still using the postgresql adapter to access my mysql models!

问题是,当我尝试访问 mysql 模型时,出现以下错误!堆栈跟踪显示,它仍在使用 postgresql 适配器访问我的 mysql 模型!

RuntimeError: ERROR C42P01  Mrelation "categories" does not exist   P15 F.\src\backend\parser\parse_relation.c  L886    RparserOpenTable: SELECT * FROM "categories" 

STACKTRACE
===========
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:507:in `execute'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:985:in `select_raw'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:972:in `select'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:81:in `cache_sql'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:661:in `find_by_sql'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1553:in `find_every'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:615:in `find'
D:/ROR/Aptana/dedomenon/app/models/category.rb:50:in `get_all_with_exclusive_scope'
D:/ROR/Aptana/dedomenon/app/models/category.rb:50:in `get_all_with_exclusive_scope'
D:/ROR/Aptana/dedomenon/app/controllers/categories_controller.rb:48:in `index'

here is my database.ymlfile

这是我的database.yml文件

postgre: &postgre
  adapter: postgresql
  database: codex
  host: localhost
  username: postgres
  password: root
  port: 5432  

mysql: &mysql
  adapter: mysql
  database: project
  host: localhost
  username: root
  password: root
  port: 3306  

development:
  <<: *postgre

test:
  <<: *postgre

production:
  <<: *postgre

master_database:
  <<: *mysql

and my master_databasemodel is like this

我的master_database模型是这样的

class Category < ActiveRecord::Base

  delegates_connection_to :master_database, :on => [:create, :save, :destroy]

end

Anyone has any solution??

任何人有任何解决方案?

回答by Hoang Tran

Another way:

其它的办法:

class Abc < ActiveRecord::Base
  establish_connection Rails.configuration.database_configuration["test"]
end

回答by Steven Soroka

might want to check out https://github.com/tchandy/octopusnowadays.

现在可能想查看https://github.com/tchandy/octopus

回答by Ken

This will change the database connection for a single model object.

这将更改单个模型对象的数据库连接。

$config = YAML.load_file(File.join(File.dirname(__FILE__),
   '../config/database.yml'))

class ModelWithDifferentConnection < ActiveRecord::Base
  establish_connection $config['connection_name_from_database_yml']
end

If you are using the same server but just a different database file then you can do something like this instead.

如果您使用相同的服务器但只是不同的数据库文件,那么您可以执行类似的操作。

class ModelWithDifferentConnection < ActiveRecord::Base

  # Lives in the CURRICULUM database
  def self.table_name
    "database.table"
  end

end

回答by Napoleon

I highly suggest MyReplicationplugin for MySQL adapter which helps you switch the connection at run-time in an elegant way:

我强烈建议MySQL 适配器的MyReplication插件,它可以帮助您在运行时以优雅的方式切换连接:

User.using(:another_database) do
  u = User.all
end

https://github.com/minhnghivn/my_replication

https://github.com/minhnghivn/my_replication

回答by karle

I've also had to connect to, and manage, two different databases, so I created a gem called secondbase: http://github.com/karledurante/secondbase

我还必须连接和管理两个不同的数据库,所以我创建了一个名为 secondbase 的 gem:http://github.com/karledurante/secondbase

回答by karle

I tried ur Sample,still getting error!!

我试过你的样品,仍然出错!!

superclass mismatch for class MysqlAdapter

I think ,the problem is with my database.ymlfile .Please check this file

我想,问题出在我的database.yml文件上。请检查这个文件

database_mysql: 
  adapter: mysql
  database: project
  host: localhost
  username: root
  password: root
  port: 3306  

development:
  adapter: postgresql
  database: codex
  host: localhost
  username: postgres
  password: root
  port: 5432  

test:
  adapter: postgresql
  database: codex
  host: localhost
  username: postgres
  password: root
  port: 5432  

production:
  adapter: postgresql
  database: codex
  host: localhost
  username: postgres
  password: root
  port: 5432  

i start the mongrel in developemnet mode only.

我只在开发网络模式下启动杂种。

here is my model superclass

这是我的模型超类

$config = YAML.load_file(File.join(File.dirname(__FILE__),
   '../../config/database.yml'))

class MasterDatabase < ActiveRecord::Base
    self.abstract_class = true
    establish_connection $config['database_mysql']    
end 

Please correct me..

请纠正我..

回答by nitecoder

I don't know about active_delegate, but I recently had to access different databases for work applications, and nothing really fit what I wanted. So I wrote something for myself, it's running in production applications as we speak.

我不知道 active_delegate,但我最近不得不访问不同的数据库以用于工作应用程序,但没有什么真正适合我想要的。所以我为自己写了一些东西,正如我们所说,它正在生产应用程序中运行。

Fixed Link connection_ninja

固定链接connection_ninja