title: rails migration to create a table giving "Table doesn't exist" when referencing a model
body: <p>I am using rails 5.1.4 with mysql2 adapter. </p>
  
  <p>When I try to create a table having reference to another table in the migration, it gives table doesn't exist. I don't understand why this error pops up. It's meaningless to see an error that can't help with any troubleshooting.</p>
  
  <p>I read another post (<a href="https://stackoverflow.com/questions/44550062/migration-to-create-table-raises-mysql2error-table-doesnt-exist">Migration to create table raises Mysql2::Error: Table doesn&#39;t exist</a>) and the solution proposed worked for me. I have a concern though with this solution as it proposes to replace the "references" with "integer" and adding "_id" to the class name being referenced. This makes the DB unaware about the FK constraint (as seen from the mysql executed in the log).</p>
  
  <p>Moreover, this error is happening in few migrations only. Other migrations with references are working fine.</p>
  
  <p>As stated before, the solution that worked doesn't seem right to me.</p>
  
  <p>The migration code that failed is this:</p>
  
  <pre><code>class CreateLocatableEntitiesPlaceEntitiesPlaces &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :locatable_entities_place_entities_places do |t|
        t.string :name
        t.integer :type
        t.references :locality, foreign_key: true, index: {:name =&gt; "index_places_on_locality_id"} 
        t.references :establishment, foreign_key: true, index: {:name =&gt; "index_places_on_establishment_id"} 
        t.references :parking, foreign_key: true, index: {:name =&gt; "index_places_on_parking_id"} 
        t.boolean :show_in_map
        t.boolean :show_locality_name
        t.date :constructed_on
        t.integer :total_area
        t.float :lat
        t.float :long
  
      end
    end
  end
  </code></pre>
  
  <p>Also wanted to add that I've namespaced my models in sub-folders, that's why I've manually named the indexes as they were getting too large to handle by MySQL. Just in case it has to do anything with it.</p>
  
  <p>Below is the screen-shot of my migrations folder having all migrations in order they're run.</p>
  
  <p><a href="https://i.stack.imgur.com/x7Lwo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x7Lwo.png" alt="Migrations folder of rails app"></a></p>