【问题标题】:Mysql2::Error: Table 'conversations' already existsMysql2::Error: 表“对话”已经存在
【发布时间】:2014-09-19 06:22:22
【问题描述】:

我在尝试更新 gem(邮箱)并将其删除后收到此错误。

Mysql2::Error: 表 'conversations' 已经存在: CREATE TABLE conversations (id int(11) DEFAULT NULL auto_increment PRIMARY KEY, subject varchar(255) DEFAULT '', created_at datetime NOT NULL, updated_at datetime NOT NULL)

我试过了:

  1. 捆绑 exec rake db:migrate 回滚然后 rake db:migrate
  2. 捆绑 exec rake db:migrate v= xxxxxxxx 然后 rake db:migrate
  3. 摆脱迁移文件(可行,但我不喜欢仅仅摆脱代码的想法)

我不想丢失我的数据并删除表格。 有什么想法吗?

更新:

Rake db:migrate:status

   up     20140526010413  Adduserfollowme to users
   up     20140703193119  Add vote weight to votes
   up     20140713211140  Add partime to user
   up     20140716024440  Add devise to admin users
   up     20140716024510  Create active admin comments
   up     20140727042518  ********** NO FILE **********
   up     20140727051902  Add slug to activities
  down    20140728031732  Create mailboxer.mailboxer engine
  down    20140728031733  Add notified object.mailboxer engine
  down    20140728031734  Add notification code.mailboxer engine
  down    20140728031735  Add attachments.mailboxer engine
  down    20140728031736  Rename receipts read.mailboxer engine
  down    20140728031737  Add global notification support.mailboxer engine

rake db:rollback

==  AddSlugToActivities: reverting ============================================
-- remove_index("activities", {:column=>:slug})
   -> 0.0215s
-- remove_column("activities", :slug)
   -> 0.0342s
==  AddSlugToActivities: reverted (0.0559s) ===================================

Rake db:migrate

==  AddSlugToActivities: migrating ============================================
-- add_column(:activities, :slug, :string)
   -> 0.0266s
-- add_index(:activities, :slug)
   -> 0.0104s
==  AddSlugToActivities: migrated (0.0373s) ===================================

==  CreateMailboxer: migrating ================================================
-- create_table(:conversations)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'conversations' already exists: CREATE TABLE `conversations` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `subject` varchar(255) DEFAULT '', `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `query'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `block in execute'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `execute'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/schema_statements.rb:14:in `create_table'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:432:in `create_table'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:466:in `block in method_missing'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:438:in `block in say_with_time'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:438:in `say_with_time'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:458:in `method_missing'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:334:in `method_missing'
/home/bill/apps/woomatch/releases/20140728100705/db/migrate/20140728031732_create_mailboxer.mailboxer_engine.rb:6:in `up'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:370:in `up'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:410:in `block in migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:389:in `migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:528:in `migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:777:in `call'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:777:in `ddl_transaction'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:719:in `block in migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:700:in `each'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:700:in `migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:570:in `up'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:551:in `migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate

接下来我该怎么做?

【问题讨论】:

  • 您是否从 0.11.0(或次要)更新到 0.12.0?
  • Warantesbr...是的,但我必须删除 0.12.0 并重新安装 0.11.0,因为我一直有错误。

标签: ruby-on-rails gem migration


【解决方案1】:

没有rake db:migrate rollback,只有rake db:rollback 尝试rake db:migrate:status,然后使用rake db:rollback 使不需要的迁移停止。

有时当'出现问题'并且您经常迁移回滚时,有必要注释一些行,使迁移\回滚并再次注释它们。

如果它不起作用,请尝试显示失败的迁移代码

【讨论】:

  • 好的...我刚刚重读了您的答案。我评论了向下迁移中的所有代码。然后再次 rake db:migrate .. 没有错误。然后 raked:migrate:status 一切都在第一列。然后一切都照常工作!感谢您的回答!
  • 没问题。但是您应该记住,这些步骤(注释,取消注释) - 意味着出了问题:也许您手动编辑了数据库,或者没有正确指定 UP 和 DOWN 迁移等。在完美的世界中,这不应该发生,您必须得到足够的回滚\迁移
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
  • 2015-05-23
  • 2018-09-22
相关资源
最近更新 更多