Mysql2::Error: Table 'table_name' doesn't exist: SHOW FULL FIELDS FROM `table_name`
It is likely caused by ActiveRecord::Migration[5.1]. When you want to add foreign key to companies
table, Migration read the tables first, but it doesn't exist.
Changing Migration to 5.0 works for me.
class CreateCompanies < ActiveRecord::Migration[5.0]
end
Or remove foreign options at first, and manually add foreign key later :
class CreateCompanies < ActiveRecord::Migration[5.1]
def change
create_table :companies do |t|
t.string :ragione_sociale
t.references :forma_giuridica # without :foreign_key option
# ...
end
# add foreign key here
add_reference :companies, :forma_giuridica
end
end
::Migration[5.0] uses primary key int(11)
and
::Migration[5.1] uses primary key bigint(20)
The most common reasons are that when creating a foreign key, both the referenced field and the foreign key field need to match:
- Engine should be the same e.g. InnoDB
- Datatype should be the same, and with same length.
e.g. VARCHAR(20) or INT(10) UNSIGNED - Collation should be the same. e.g. utf8
- Unique - Foreign key should refer to field that is unique (usually private) in the reference table.