Mysql2::Error: Table 'table_name' doesn't exist: SHOW FULL FIELDS FROM `table_name`
Mysql2::Error: Table 'table_name' doesn't exist: SHOW FULL FIELDS FROM `table_name`
我正在使用 rails 5.1.1 和 mysql 5.7。我有一个迁移 class:
class CreateCompanies < ActiveRecord::Migration[5.1]
def change
create_table :companies do |t|
t.string :ragione_sociale
t.references :forma_giuridica, foreign_key: true
t.string :telefono_fisso
t.string :fax
t.string :pec
t.string :mail
t.string :web_site
t.string :cellulare_emergenze
t.string :via_legale
t.string :n_civico_legale
t.string :citta_legale
t.string :cap_legale
t.string :provincia_legale
t.string :regione_legale
t.string :via_operativa
t.string :n_civico_operativa
t.string :citta_operativa
t.string :cap_operativa
t.string :provincia_operativa
t.string :regione_operativa
t.string :pi
t.string :cf
t.string :ateco_primo
t.string :ateco_secondo
t.string :ateco_altro
t.timestamps
end
end
end
我有一个空模型 calledny.rb
和一个名为 companies_controller.rb
的控制器。
我的database.yml
是:
# MySQL. Versions 5.1.10 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.7/en/old-client.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: Pipipopo09
host: localhost
port: 3300
development:
<<: *default
database: Pluto_Demetra_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: Pluto_Demetra_test
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
database: Pluto_Demetra_production
username: Pluto_Demetra
password: <%= ENV['PLUTO_DEMETRA_DATABASE_PASSWORD'] %>
当运行 rails db:migrate
出现以下错误:
== 20170621125622 CreateCompanies: migrating ==================================
-- create_table(:companies)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'pluto_demetra_development.companies' doesn't exist: SHOW FULL FIELDS FROM `companies`
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'pluto_demetra_development.companies' doesn't exist: SHOW FULL FIELDS FROM `companies`
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Mysql2::Error: Table 'pluto_demetra_development.companies' doesn't exist
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Mysql2::Error: Cannot add foreign key constraint
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
我该如何解决这个错误?
可能是ActiveRecord::Migration[5.1]造成的。当你想给 companies
table 添加外键时,Migration 首先读取 table,但它不存在。
将迁移更改为 5.0 对我有效。
class CreateCompanies < ActiveRecord::Migration[5.0]
end
或者先去掉外键,以后再手动添加外键:
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] 使用主键 int(11)
和
::Migration[5.1] 使用主键 bigint(20)
最常见的原因是创建外键时,引用字段和外键字段都需要匹配:
- 引擎应该相同例如InnoDB
- 数据类型应该相同,并且长度相同。
例如VARCHAR(20) 或 INT(10) 无符号
- 排序规则应该相同。 例如utf8
- Unique - 外键应引用引用 table 中唯一的字段 (通常是私有的)。
如其他答案中所述,当您使用的 ActiveRecord::Migration 版本对目标 [=22= 上的 id
字段的类型做出错误假设时,您可能会收到此错误] table.
特别是,从 5.1 版开始,ActiveRecord 假定 id 字段的类型为 bigint
,而不是以前版本中的类型 int
。
您可以通过在迁移中手动指定外键的目标字段为 type: :int
来解决此问题,如下所示:
class MyMigration < ActiveRecord::Migration[5.2]
def change
create_table :my_new_table do |t|
t.references :my_existing_table,
foreign_key: true,
type: :int
end
end
end
我正在使用 rails 5.1.1 和 mysql 5.7。我有一个迁移 class:
class CreateCompanies < ActiveRecord::Migration[5.1]
def change
create_table :companies do |t|
t.string :ragione_sociale
t.references :forma_giuridica, foreign_key: true
t.string :telefono_fisso
t.string :fax
t.string :pec
t.string :mail
t.string :web_site
t.string :cellulare_emergenze
t.string :via_legale
t.string :n_civico_legale
t.string :citta_legale
t.string :cap_legale
t.string :provincia_legale
t.string :regione_legale
t.string :via_operativa
t.string :n_civico_operativa
t.string :citta_operativa
t.string :cap_operativa
t.string :provincia_operativa
t.string :regione_operativa
t.string :pi
t.string :cf
t.string :ateco_primo
t.string :ateco_secondo
t.string :ateco_altro
t.timestamps
end
end
end
我有一个空模型 calledny.rb
和一个名为 companies_controller.rb
的控制器。
我的database.yml
是:
# MySQL. Versions 5.1.10 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.7/en/old-client.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: Pipipopo09
host: localhost
port: 3300
development:
<<: *default
database: Pluto_Demetra_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: Pluto_Demetra_test
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
database: Pluto_Demetra_production
username: Pluto_Demetra
password: <%= ENV['PLUTO_DEMETRA_DATABASE_PASSWORD'] %>
当运行 rails db:migrate
出现以下错误:
== 20170621125622 CreateCompanies: migrating ==================================
-- create_table(:companies)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'pluto_demetra_development.companies' doesn't exist: SHOW FULL FIELDS FROM `companies`
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'pluto_demetra_development.companies' doesn't exist: SHOW FULL FIELDS FROM `companies`
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Mysql2::Error: Table 'pluto_demetra_development.companies' doesn't exist
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Mysql2::Error: Cannot add foreign key constraint
C:/Users/gvieri.BSNSTRATEGIES/SitiRuby/Pluto_Demetra/db/migrate/20170621125622_create_companies.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
我该如何解决这个错误?
可能是ActiveRecord::Migration[5.1]造成的。当你想给 companies
table 添加外键时,Migration 首先读取 table,但它不存在。
将迁移更改为 5.0 对我有效。
class CreateCompanies < ActiveRecord::Migration[5.0]
end
或者先去掉外键,以后再手动添加外键:
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] 使用主键 int(11)
和
::Migration[5.1] 使用主键 bigint(20)
最常见的原因是创建外键时,引用字段和外键字段都需要匹配:
- 引擎应该相同例如InnoDB
- 数据类型应该相同,并且长度相同。
例如VARCHAR(20) 或 INT(10) 无符号 - 排序规则应该相同。 例如utf8
- Unique - 外键应引用引用 table 中唯一的字段 (通常是私有的)。
如其他答案中所述,当您使用的 ActiveRecord::Migration 版本对目标 [=22= 上的 id
字段的类型做出错误假设时,您可能会收到此错误] table.
特别是,从 5.1 版开始,ActiveRecord 假定 id 字段的类型为 bigint
,而不是以前版本中的类型 int
。
您可以通过在迁移中手动指定外键的目标字段为 type: :int
来解决此问题,如下所示:
class MyMigration < ActiveRecord::Migration[5.2]
def change
create_table :my_new_table do |t|
t.references :my_existing_table,
foreign_key: true,
type: :int
end
end
end