迁移到创建表会引发Mysql2 :: Error:表不存在 [英] Migration to create table raises Mysql2::Error: Table doesn't exist
问题描述
我用以下内容编写了迁移:
I wrote a migration with the following:
class CreateTableSomeTable < ActiveRecord::Migration[5.1]
def change
create_table :some_tables do |t|
t.references :user, foreign_key: true
t.references :author, references: :user, foreign_key: true
t.text :summary
end
end
end
这是创建数据库表的基本迁移.但是:当我运行rails db:migrate
时,一个非常奇怪的错误消息会中止迁移:
It is a basic migration that is creating a database table. However: when I run rails db:migrate
a very odd error message aborts the migration:
Mysql2 :: Error:表'my_database.some_tables'不存在:从'some_tables'中显示完整字段
Mysql2::Error: Table 'my_database.some_tables' doesn't exist: SHOW FULL FIELDS FROM 'some_tables'
错误似乎是由于表确实存在而无法创建表,这没有任何意义.
It is as if the error is saying it can't create the table because the table does exist, which doesn't make sense.
我查看并尝试过的东西:
Things I have looked at and tried:
- 查看了database.yml,看起来不错.一切都没有改变,我最近也进行了其他迁移(尽管没有迁移可以创建数据库表).
- 运行
bundle
以确保所有宝石都已安装 - 删除了
schema.rb
文件,使用来自另一个副本的数据重新创建了数据库,然后我运行rake db:schema:dump
来重新创建schema.rb
文件.我尝试再次运行迁移,但仍然遇到相同的错误.
- reviewed the database.yml which seems fine. Nothing has changed, and I have recently run other migrations just fine (though no migrations that created database tables)
- ran
bundle
to ensure all gems were installed - deleted the
schema.rb
file, recreated the database with data from another copy, and I ranrake db:schema:dump
to recreate theschema.rb
file. I attempted to run the migration again and still got the same error.
我正在使用rails 5.1.1
和mysql2 0.4.6
有关如何使迁移运行的任何提示?
Any tips on how I can get the migration to run?
推荐答案
我想出了一个解决方法,但是这仍然让我感到困惑.
I figured out a work around, but it is still very puzzling to me.
日志文件中的错误消息未完全指出问题所在.由于某些原因,它可能是rails 5.1.1
或可能是mysql2 0.4.6
,但是由于某些原因,它不喜欢在create_table
块中使用references
.很奇怪,因为它过去对我有用.
The error message in the log file was not exactly pointing to the issue. For some reason, it might be rails 5.1.1
or it might be mysql2 0.4.6
, but it doesn't like using references
within the create_table
block for some reason. Very odd because it has worked for me in the past.
因此,我从此更改了迁移:
So I changed the migration from this:
class CreateTableSomeTable < ActiveRecord::Migration[5.1]
def change
create_table :some_tables do |t|
t.references :user, foreign_key: true
t.references :author, references: :user, foreign_key: true
t.text :summary
end
end
end
对此:
class CreateTableSomeTable < ActiveRecord::Migration[5.1]
def change
create_table :some_tables do |t|
t.integer :user_id
t.integer :author_id
t.text :summary
end
end
end
它奏效了.
这很奇怪,因为references
在sqlite3
上可以正常工作(我通过生成一个虚拟应用程序对此进行了测试,并通过一个references
列运行了一个scaffold命令,然后运行了rails db:migrate
,这一切都可以工作).
It is very odd because references
works just fine with sqlite3
(I tested this by generating a dummy app, ran a scaffold command with a references
column, and ran rails db:migrate
and it all worked).
这篇关于迁移到创建表会引发Mysql2 :: Error:表不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!