title: Understanding Rails 6 with multiple database
body: <pre><code>Rails 6 API
  ruby 3
  </code></pre>
  <p>This is my first time using Rails with more than one databases but I struggle to understand how it all works. My &quot;playground&quot; is simple:</p>
  <ul>
  <li>A primary database</li>
  <li>A secondary (not a replica)</li>
  </ul>
  <p>The secondary database is used for &quot;putting stuff away&quot; and access it whenever needs be.</p>
  <p><strong>database.yml:</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>default: &amp;default
    adapter: mysql2
    encoding: utf8mb4
    pool: &lt;%= ENV.fetch(&quot;RAILS_MAX_THREADS&quot;) { 5 } %&gt;
    username: root
    password:
    socket: /tmp/mysql.sock
  
  development:
    primary:
      &lt;&lt;: *default
      database: primary_development
      password: password
    secondary:
      &lt;&lt;: *default
      database: secondary_development
      password: password
  
  </code></pre>
  <p>Based on the <a href="https://guides.rubyonrails.org/active_record_multiple_databases.html" rel="nofollow noreferrer">docs</a>, I came up with the above.</p>
  <p>Still per docs:</p>
  <blockquote>
  <p>Now that we have a new database, let's set up the connection model. In order to use the new database we need to create a new abstract class and connect to the animals databases.</p>
  </blockquote>
  <pre class="lang-rb prettyprint-override"><code>
  # /models/secondary_record.rb
  class SecondaryRecord &lt; ApplicationRecord
      self.abstract_class = true
    
      connects_to database: { writing: :secondary, reading: :secondary }
  end
  
  </code></pre>
  <p>And...</p>
  <pre class="lang-rb prettyprint-override"><code>class ApplicationRecord &lt; ActiveRecord::Base
    self.abstract_class = true
  
    connects_to database: { writing: :primary, reading: :primary }
  end
  </code></pre>
  <p>This is a generic model:</p>
  <pre class="lang-rb prettyprint-override"><code>class Order &lt; SecondaryRecord
      #...
  end
  </code></pre>
  <p>Everything looks so far. The migration:</p>
  <pre class="lang-sh prettyprint-override"><code>rails g migration create_orders --database secondary
  </code></pre>
  <p>Before this setup, I'd already have a &quot;primary&quot; database so I searched online how to connect additional databases. After running <code>rails db:migrate</code> my primary database went ahead and create a new <code>orders</code> table. I never expected that. More digging I ran, after rolling back:</p>
  <blockquote>
  <p>rails db:migrate:secondary</p>
  </blockquote>
  <p>This seems better. A new table was created in the <code>secondary</code> database. Here's the issue... restarting the server I get an error &quot;migration pending&quot;. This is where I'm lost. Does anyone has a blog or can deeply explain how to connect multiple databases and run migrations for a specific database? Do I need to delete the app and start again?</p>
  <p>EDIT:</p>
  <p>When ran <code>rails g migration create_orders --database secondary</code>, it produces:</p>
  <pre class="lang-rb prettyprint-override"><code>class CreateOrders &lt; ActiveRecord::Migration[6.1]
    def change
      create_table :orders do |t|
  
        t.timestamps
      end
    end
  end
  </code></pre>
  <p>Nothing related to the secondary database with flag <code>--database secondary</code>.</p>