Rails 中的多个数据库连接
我在 Rails 中使用 active_delegate 进行多个连接。这里我使用 mysql 作为某些模型的 master_database ,并使用 postgresql 作为其他一些模型的 master_database 。
问题是,当我尝试访问 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'
这是我的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
,我的master_database模型是这样的
class Category < ActiveRecord::Base
delegates_connection_to :master_database, :on => [:create, :save, :destroy]
end
任何人都有解决方案吗?
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.
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!
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.yml
file
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_database
model is like this
class Category < ActiveRecord::Base
delegates_connection_to :master_database, :on => [:create, :save, :destroy]
end
Anyone has any solution??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
另一种方式:
Another way:
现在可能想查看https://github.com/tchandy/octopus。
might want to check out https://github.com/tchandy/octopus nowadays.
这将更改单个模型对象的数据库连接。
如果您使用相同的服务器但只是不同的数据库文件,那么您可以执行类似的操作。
This will change the database connection for a single model object.
If you are using the same server but just a different database file then you can do something like this instead.
我强烈建议 MySQL 适配器使用 MyReplication 插件,它可以帮助您以优雅的方式在运行时切换连接:
https://github.com/minhnghivn/my_replication
I highly suggest MyReplication plugin for MySQL adapter which helps you switch the connection at run-time in an elegant way:
https://github.com/minhnghivn/my_replication
我尝试了你的示例,仍然出现错误!
我认为,问题出在我的
database.yml
文件上。请检查此文件,我仅在developemnet 模式下启动 mongrel。
这是我的模型超类
请纠正我..
I tried ur Sample,still getting error!!
I think ,the problem is with my
database.yml
file .Please check this filei start the mongrel in developemnet mode only.
here is my model superclass
Please correct me..
我还必须连接并管理两个不同的数据库,因此我创建了一个名为 secondarybase 的 gem: http:// /github.com/karledurante/secondbase
I've also had to connect to, and manage, two different databases, so I created a gem called secondbase: http://github.com/karledurante/secondbase
我不了解 active_delegate,但我最近不得不访问不同的工作应用程序数据库,但没有什么真正符合我想要的。所以我为自己写了一些东西,正如我们所说,它正在生产应用程序中运行。
固定链接 connection_ninja
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.
Fixed Link connection_ninja