如何使用数据库.yml在Rails应用程序中使用多个数据库[英] How to Use Multiple Databases in a Rails App Using the database.yml

本文是小编为大家收集整理的关于如何使用数据库.yml在Rails应用程序中使用多个数据库的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经阅读了有关如何执行此操作的文档,但是实际上,我有问题. 在我的应用程序中,我有2个不同的数据库,如下面的数据库.

sqlite_test:
    adapter: sqlite3
    database: db/sqlite_test.sqlite3
    table: plots
    pool: 5
    timeout: 5000

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: test
  pool: 5
  username: myname
  password: mypassword
  host: localhost

我的应用程序是一个动态绘图仪,它将在(基本)数据库中绘制数据,而无需了解数据库中的内容或其结构化的方式.这两个数据库都包含不同的数据.我在单独的Rails应用中创建的SQLite数据库.

我正在使用的当前应用程序是围绕MySQL数据库构建的,该数据库是我在外部构建的. 我将SQLITE数据库复制到/DB目录中.因此,在我的主要模型中,当我说:

  class Plot < ActiveRecord::Base

  establish_connection :development
  set_table_name "stock_test"
  set_primary_key :id

一切都很好,花花公子.但是,当我将其更改为:

 establish_connection :sqlite_test
 set_table_name "plots"

并尝试通过导轨控制台访问该数据库,我会发现一个错误说:

>>ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter

我不知道为什么是这样,因为database.yml文件清楚地指定了适配器? 但是,当我手工制作模型时,一切都可以正常工作.

class Plot < ActiveRecord::Base
establish_connection(:adapter => "sqlite3", :database => "db/sqlite_test.sqlite3", :pool => 5 )

当我手动指定数据库中的内容时,为什么它们都可以工作,但是当我只使用database.yml引用时?

时不行

谢谢!

推荐答案

我试图复制并使其工作.它与命名约定有关: 您处于开发模式,AR将寻找开发标签,在您的情况下,Sqlite不存在.

这是我的数据库:

development:
  adapter: mysql2
  database: se_development
  username: root
  pool: 5
  timeout: 5000

sqlite_development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

这是模型:

class Plot < ActiveRecord::Base
  establish_connection 'sqlite_' + Rails.env
end

为我工作.希望这会有所帮助.

本文地址:https://www.itbaoku.cn/post/597769.html

问题描述

I've read up on the documentation on how to do this, but in practice, I am having problems. In my app, I have 2 different databases as described below in my database.yml file.

sqlite_test:
    adapter: sqlite3
    database: db/sqlite_test.sqlite3
    table: plots
    pool: 5
    timeout: 5000

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: test
  pool: 5
  username: myname
  password: mypassword
  host: localhost

My application is a dynamic plotter that will plot the data in a (basic) database without having knowledge of whats in the database, or how its structured. Both of these databases contain different data. The SQLite database I created in a separate Rails app.

The current app I'm using is built around the MYSQL database, which I build externally. I copied the SQLite database into the /db directory. So in my main model, when I say:

  class Plot < ActiveRecord::Base

  establish_connection :development
  set_table_name "stock_test"
  set_primary_key :id

Everything works out just fine and dandy. However, when I change it to:

 establish_connection :sqlite_test
 set_table_name "plots"

and try to access that database via the Rails console, I get an error saying:

>>ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter

I don't know why that is, since the database.yml file clearly does specify an adapter? When I do it by hand in my model though, everything works exactly as it should.

class Plot < ActiveRecord::Base
establish_connection(:adapter => "sqlite3", :database => "db/sqlite_test.sqlite3", :pool => 5 )

Why does it all work when I manually specify whats in the database.yml, but not when I just use the database.yml reference?

Thanks!

推荐答案

I tried to replicate and got it to work. It has to do with naming conventions: You're in development mode and AR will look for the development tag, which in your case does not exist for sqlite.

Here is my database.yml:

development:
  adapter: mysql2
  database: se_development
  username: root
  pool: 5
  timeout: 5000

sqlite_development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

Here is the model:

class Plot < ActiveRecord::Base
  establish_connection 'sqlite_' + Rails.env
end

Worked for me. Hope this helps.