[英]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这是我的 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)
class User < ApplicationRecord
establish_connection :auxillary
end
Or You can change the DB directrly in the code(eg rails console)或者您可以直接在代码中更改数据库(例如 rails 控制台)
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:也许您需要将 DB 定义为:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.