Search code examples
ruby-on-railsrails-migrations

rails migration to create a table giving "Table doesn't exist" when referencing a model


I am using rails 5.1.4 with mysql2 adapter.

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.

I read another post (Migration to create table raises Mysql2::Error: Table doesn't exist) 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).

Moreover, this error is happening in few migrations only. Other migrations with references are working fine.

As stated before, the solution that worked doesn't seem right to me.

The migration code that failed is this:

class CreateLocatableEntitiesPlaceEntitiesPlaces < 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 => "index_places_on_locality_id"} 
      t.references :establishment, foreign_key: true, index: {:name => "index_places_on_establishment_id"} 
      t.references :parking, foreign_key: true, index: {:name => "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

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.

Below is the screen-shot of my migrations folder having all migrations in order they're run.

Migrations folder of rails app


Solution

  • I realized what went wrong with my initial code. Setting foreign_key as true in the migration required the table to be discoverable. As the name of the table wasn't obvious from the name specified in the references, it gave this error.

    In rails 5+, you can specify the table name to which the key should refer to. After making this change, I was able to run the migrations without any issues.

    Below is the updated code:

    class CreateLocatableEntitiesPlaceEntitiesPlaces < 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: {to_table: :base_entities_locality_entities_localities}, index: {:name => "index_places_on_locality_id"}
          t.references :establishment, foreign_key: {to_table: :locatable_entities_place_entities_establishments}, index: {:name => "index_places_on_establishment_id"} 
          t.references :parking, foreign_key: {to_table: :locatable_entities_place_entities_parkings}, index: {:name => "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
    

    As mentioned in my question, I name-spaced my models, this was the reason for the lack of obviousness of the table names that rails couldn't find by itself.

    This post helped in resolving this issue.: Specifying column name in a "references" migration