title: Rails 5 - Multiple Foreign keys belongs to the same table
body: <p>I have a Order model that belongs to a LoadingStation model. And the the LoadingStation will be used two times in the Order table, so it looks like: </p>
  
  <pre><code>class CreateLoadingStations &lt; ActiveRecord::Migration[5.0]
    def change
      create_table :loading_stations do |t|
        t.integer :type
        t.string :comp_name1
        t.string :street
        t.string :street_num
        t.string :zip_code
        t.string :city
  
        t.timestamps
      end
    end
    end
  
  
  
   class CreateOrders &lt; ActiveRecord::Migration[5.0]
        def change
           create_table :orders do |t|
            t.string :status
            t.belongs_to :loading_station, class_name: "LoadingStation", index: true, foreign_key: "loading_station_id"
            t.belongs_to :unloading_station, class_name: "LoadingStation", index: true, foreign_key: "unloading_station_id"
  
            t.timestamps
          end
        end
      end
  </code></pre>
  
  <p>When I let run <strong>rails db:migrate</strong> I got this error:
  <strong>ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "unloading_stations" does not exist</strong></p>
  
  <p>Mmmmmm, seems to be that the class_name was not correct detected. The class_name should be the same in both statements, correct ? </p>
  
  <p>Let check the model of Loading Station:</p>
  
  <pre><code> class LoadingStation &lt; ApplicationRecord
  end
  </code></pre>
  
  <p>Okay, I change the CreateOrders migration:</p>
  
  <pre><code> t.belongs_to :loading_station, class_name: "LoadingStation", index: true, foreign_key: "unloading_station_id"
  </code></pre>
  
  <p>Now when I let run the rails db:migrate I got this error:
  <strong>ActiveRecord::StatementInvalid: PG::DuplicateObject: ERROR:  constraint "fk_rails_5294e269cc" for relation "orders" already exists</strong></p>
  
  <p>Okay, I understand the Foreign Key in the database seems to be identical and the database declines the migration task. </p>
  
  <p>But what is the sense of the foreign_key: option when I define who different foreign_key names, when the database detects two identical ? </p>
  
  <p><strong>Appendix:</strong></p>
  
  <p>Here my Order model:</p>
  
  <pre><code>class Order &lt; ApplicationRecord
  end
  </code></pre>
  
  <p><strong>Final Question</strong> </p>
  
  <p>And at the end - among the error messages - I want to have two Foreign Keys pointing to the same table. </p>