title: Rails 4 foreign_key is not working with belongs_to
body: <p>I am using rails 4.2.4 and I want to add a foreign key.</p>
  
  <p>User:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
     has_many :posts
     validates :name, presence: true
  end
  </code></pre>
  
  <p>Post:</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
     belongs_to :user
     validates :user, presence: true
  end
  </code></pre>
  
  <p>Migration:</p>
  
  <pre><code>class AddForeignKeysToTables &lt; ActiveRecord::Migration
     def change
        create_table :users do |t|
           t.string   :name
        end
  
        create_table :posts do |t|
           t.belongs_to :user
        end
  
       add_foreign_key :posts, :users
     end
  end
  </code></pre>
  
  <p>Migration report:</p>
  
  <pre><code> == 20160224131802 AddForeignKeysToRfmAnalyticsTables: migrating ===============
      -- create_table(:users)
         -&gt; 0.0150s
         -&gt; 0 rows
      -- create_table(:posts)
         -&gt; 0.0150s
         -&gt; 0 rows
      -- add_foreign_key(:posts, :users)
         -&gt; 0.0000s
      == 20160224131802 AddForeignKeysToTables: migrated (0.0300s) ======
  </code></pre>
  
  <p>And adding foreign_key in MySQL database is not working without errors. And not one foreign key is created. Do you have any ideas how to create foreign key in this example?</p>
  
  <p>But I want to keep belongs_to in migration, because this is like from old migration, and add_foreign_key has to be a new one migration.</p>
  
  <p>Maybe related gems:</p>
  
  <pre><code>jruby_version = '1.7.22'
  
  gem 'rails', '4.2.4'
  gem 'activerecord-jdbcmysql-adapter' 
  </code></pre>
  
  <p>Also script from schema.rb:</p>
  
  <pre><code>create_table "posts", force: :cascade do |t|
     t.integer "user_id", limit: 11
  end
  
  create_table "users", force: :cascade do |t|
     t.string "name", limit: 255
  end
  </code></pre>
  
  <p>And also if I am using gems like 'foreigner' or 'foreign_keys', then foreign keys are created, but these gem seems to be incompatible.</p>