简体   繁体   中英

How do I use the second database in Rails 6

I have this database configuration

default: &default
  adapter: oracle_enhanced
  username: dbuser
  password: secret

auxillary: &auxillary
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  migrations_paths: db/auxillary_migrate


development:
  primary:
    <<: *default
    database: DEVLIMS
  auxillary:
    <<: *auxillary
    database: db/development_auxillary.sqlite3

It works with migrations, but I get this error in the console

2.6.3 :001 > User.last
Traceback (most recent call last):
        1: from (irb):1
ArgumentError (No database file specified. Missing argument: database)
2.6.3 :002 > 

This is my model

class User < ApplicationRecord
  connects_to database: { writing: :auxillary, reading: :auxillary}
end

How am I supposed to read and write the data from the auxiliary database? I need the auxiliary database for user authentication, permissions system and other things that should not be placed in the primary database.

The answer suggested by someone did not work. It would appear something is wrong with my configuration.

2.6.3 :002 > ActiveRecord::Base.establish_connection(:auxillary)
 => #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x000055acd5119540 @mon_mutex=#<Thread::Mutex:0x000055acd51194c8>, @mon_mutex_owner_object_id=47100398717600, @mon_owner=nil, @mon_count=0, @query_cache_enabled=#<Concurrent::Map:0x000055acd51194a0 entries=0 default_proc=#<Proc:0x000055acd51193b0@/home/jacekp/.rvm/gems/ruby-2.6.3@qc_charts_ror6/gems/activerecord-6.0.3/lib/active_record/connection_adapters/abstract/query_cache.rb:32>>, @spec=#<ActiveRecord::ConnectionAdapters::ConnectionSpecification:0x000055acd51197c0 @name="primary", @config={:adapter=>"sqlite3", :pool=>5, :timeout=>5000, :migrations_paths=>"db/auxillary_migrate"}, @adapter_method="sqlite3_connection">, @checkout_timeout=5, @idle_timeout=300.0, @size=5, @thread_cached_conns=#<Concurrent::Map:0x000055acd5119360 entries=0 default_proc=nil>, @connections=[], @automatic_reconnect=true, @now_connecting=0, @threads_blocking_new_connections=0, @available=#<ActiveRecord::ConnectionAdapters::ConnectionPool::ConnectionLeasingQueue:0x000055acd5119270 @lock=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x000055acd5119540 ...>, @cond=#<MonitorMixin::ConditionVariable:0x000055acd5119248 @monitor=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x000055acd5119540 ...>, @cond=#<Thread::ConditionVariable:0x000055acd5119220>>, @num_waiting=0, @queue=[]>, @lock_thread=false, @reaper=#<ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper:0x000055acd51191d0 @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x000055acd5119540 ...>, @frequency=60.0>> 
2.6.3 :003 > ActiveRecord::Base.connection.current_database
Traceback (most recent call last):
        1: from (irb):3
ArgumentError (No database file specified. Missing argument: database)

I`m using this :

class User < ApplicationRecord
  establish_connection :auxillary
end

Or You can change the DB directrly in the code(eg rails console)

ActiveRecord::Base.establish_connection(:auxillary)
ActiveRecord::Base.connection.current_database # to check what DB is using now

Maybe You need to define DB`s as:

default: &default
  adapter: oracle_enhanced
  username: dbuser
  password: secret

auxillary: &auxillary
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  migrations_paths: db/auxillary_migrate
  database: db/development_auxillary.sqlite3

development:
  <<: *default
  database: DEVLIMS

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