简体   繁体   中英

DB connection problem in production

I have a separate DB for one model in my application and in development mode the connection is working properly, in production however it isn't.

production:
  adapter: mysql
  host: myhost
  username: root
  password:
  database: production_db

users_production:
  adapter: mysql
  host: myhost
  username: root
  password:
  database: other_db

The model that connects to the other database is called User but the table it references in other_db is smf_users so my User.rb looks like this:

class User < ActiveRecord::Base
  establish_connection "users_#{RAILS_ENV}"
  set_table_name "smf_users"
end

In production I'm getting this error:

Mysql::Error: Table 'production_db. smf_users' doesn't exist:

Note how it is trying to connect to the wrong database and so isn't finding the correct table. As I say, this works in development mode.

Any suggestions?

I've found when using multiple databases that odd errors show up when doing associations. Any chance you've got another model out there which belongs_to :users and you're expecting it to Just Work? Otherwise, you have to look at caching -- it's easily possible that Rails is failing to cache the connection data for your extra database correctly.

Try:

establish_connection configurations[RAILS_ENV]["users_#{RAILS_ENV}"]
User.connection

establish_connection needs a hash of the connection information. This should return if from database.yml.

You might want to check your log for a message like the following:

"users_production database is not configured"

That message would be thrown by ActiveRecord::Base if it can't find the congifuration by string in database.yml.

您可能会发现这很有用: http : //magicmodels.rubyforge.org/magic_multi_connections/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM