【问题标题】:rake db:migrate ERROR for "Table already exists"rake db:“表已存在”的迁移错误
【发布时间】:2014-07-10 19:01:02
【问题描述】:

我使用 capistrano 3.1 部署到 prod 服务器。

以前,它工作正常。但是今天,当我更改一个 rb 文件(没有添加和更改数据库迁移)时,在部署过程中,当它运行到

Running /usr/local/rvm/bin/rvm default do bundle exec rake db:migrate on xxx

显示错误:

[cc1734f0]  Migrating to CreateDevices (20140416233606)
DEBUG [cc1734f0]    == 20140416233606 CreateDevices: migrating ====================================
DEBUG [cc1734f0]    
DEBUG [cc1734f0]    -- create_table(:devices)
DEBUG [cc1734f0]    
DEBUG [cc1734f0]    Mysql2::Error: Table 'devices' already exists: CREATE TABLE `devices` (`id` int(11) auto_increment PRIMARY KEY, `soh_id` varchar(255), `token_id` varchar(255), `device_hw` varchar(255), `device_sw` varchar(255), `device_name` varchar(255), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
 DEBUG [cc1734f0]   rake aborted!

但在我的 schema.rb 中,我有:

ActiveRecord::Schema.define(version: 20140513035458) do

因为我上次的部署已经创建了表,所以现在报错了。

那么为什么 rake db:migrate 运行之前又执行过的迁移!

我不能删除表格,因为它已经包含数据了。

【问题讨论】:

    标签: ruby-on-rails rake capistrano migrate


    【解决方案1】:

    如果表不存在,则更新您的迁移以有条件地执行

     class MyMigration < ActiveRecord::Migration
       def change
         unless table_exists? :devices
          #create the table
         end
      end
     end
    

    【讨论】:

      【解决方案2】:

      解决此问题的一种方法是将“20140416233606”添加到 schema_migration 表中。然后 Rails 将不会运行此迁移。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-05
        • 1970-01-01
        • 2016-01-19
        • 2011-11-06
        • 2011-04-29
        • 2012-06-16
        相关资源
        最近更新 更多