2

最近我将 PostgreSQL 添加到我已经在 MySQL 上运行的应用程序中,现在应用程序在两个数据库上运行。我能够在不同数据库中存在的表及其工作正常之间建立关联。今天我在一个页面中添加了一个搜索功能,该功能使用两个数据库中存在的表并引发错误。花了一整天试图找出错误,但没有成功。请查看关联,代码并帮助我更正它。

我有 4 张桌子:-

公司(mysql)

位置(mysql)

报告(PostgreSQL)

报告类别(PostgreSQL)

模型 - company.rb

establish_connection Rails.env+"_postgres"
has_many :reports

establish_connection Rails.env
belongs_to :location

位置.rb

has_many :companies

报告.rb

establish_connection Rails.env
belongs_to :company

establish_connection Rails.env+"_postgres"
belongs_to :report_category

报告类别.rb

establish_connection Rails.env+"_postgres"
has_many :report

现在从视图中我传递搜索参数并在我的报告控制器中写

@reports = Report.where("companies.name like ? and report_category.name ?", params[:company], params[:category]).includes(:company, :report_category)

执行此行后,我收到以下错误

ActiveRecord::StatementInvalid: PGError: ERROR:  relation "companies" does not exist

Company.where("location.name like ?", params[:location]).includes(:location)

或者

Report.where("report_categories.name like ?", params[:category]).includes(:report_category)

工作得很好。如果我使用两个数据库进行搜索,我只会收到错误消息。请帮忙

4

1 回答 1

0

First I want to second the suggestion in the comments that you move everything onto one db. This saves you a lot of complexity down the road. However if you really can't do this, there are a couple ways you can access the MySQL tables from within PostgreSQL, and this would be a second-class solution to a full migration, but it would work.

In PostgreSQL 9.1 and higher you can do read-only queries using foreign data wrappers for MySQL. In earlier versions you can use David Fetter's DBI-Link project to make the connection.

These then allow you to present your data as a unified database to your applications.

于 2013-03-24T04:53:29.573 回答