【问题标题】:How to fix Duplicate Migration Name Error如何修复重复的迁移名称错误
【发布时间】:2014-10-20 07:26:35
【问题描述】:

我一直在 SO 和 google 上研究这个问题,但我找不到任何可以告诉我如何删除特定迁移而不加载到 Heroku 中的信息。

我的问题是我在同一个项目中创建了多个迁移(我正在学习一个教程,我认为我跳过了一个步骤,而实际上我没有)现在当我试图将此数据库迁移到 Heroku我收到重复的迁移错误:

Running `rake db:migrate` attached to terminal... up, run.7535
rake aborted!
ActiveRecord::DuplicateMigrationNameError:

Multiple migrations have the name CreateUserFriendships

/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:1011:in `validate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:917:in `initialize'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:807:in `new'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:807:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:785:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我很久以前删除了我的应用程序目录中的重复迁移文件以启动我的项目并在我的本地计算机上运行,​​现在在部署它时仍然引用这个重复但我不确定我需要去哪里删除它.

有人可以告诉我在哪里/如何删除这些重复的迁移吗?

谢谢, 编辑:我成功删除了其中一个迁移,但是当我运行“heroku run rake db:migrate”时,现在出现了一个不同的错误(在提交并推送到 heroku 之后):

Running `rake db:migrate` attached to terminal... up, run.2527
== 20141004220407 AddStateToUserFriendships: migrating ========================
-- add_column(:user_friendships, :state, :string)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "user_friendships" does not exist
: ALTER TABLE "user_friendships" ADD COLUMN "state" character varying(255)/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in `execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/schema_statements.rb:375:in `add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql/schema_statements.rb:395:in `add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:649:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:621:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:621:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:641:in `method_missing'
/app/db/migrate/20141004220407_add_state_to_user_friendships.rb:3:in `change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:595:in `exec_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:578:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:577:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:752:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:992:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:1038:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:208:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:1038:in `ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:991:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:953:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:949:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:949:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:807:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/migration.rb:785:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我现在需要重新添加旧迁移吗?其中一个迁移的运行时间比另一个要晚,我应该将原来的迁移添加回我的程序文件并再次提交吗?

【问题讨论】:

    标签: ruby-on-rails heroku


    【解决方案1】:

    确保更改已提交并且文件不再在您的 git 存储库中。

    如果你在项目中运行git status,它可能会这样写:

    # On branch master
    # Changed but not updated:
    #   (use "git add/rm <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #       deleted:    db/migrate/201410170100123013_create_user_friendships.rb
    

    通过运行git add . 然后git commit -m "Deleted migration" 提交您的更改。

    尝试推送到 Heroku 并再次迁移。

    【讨论】:

    • 嗨,理查德。我确实看到迁移被标记为已删除但未在提交时更新。我做了 git rm /that_file 并提交并推送到heroku。现在,当我运行 db:migrate 时,我收到一条错误消息,指出我的关系“user_friendships”不存在。有关整个消息,请参阅我的原始问题。看起来好像我删除了那个迁移,现在它没有关于用户友谊的信息。我不小心生成了两次添加用户友谊迁移,是我删除了错误的吗?我应该删除另一个?
    • 也许是一个更简单的问题:有没有办法只在我的本地应用程序项目文件中获取架构并将其发布到我的 heroku 而不必运行这些迁移?
    【解决方案2】:

    我知道我需要做什么。显然,这些迁移按照它们的时间戳顺序运行,而我使用的重复迁移是在依赖它为其表创建某些内容的迁移之后发生的,这是有道理的。所以我重新添加了原来的迁移;确保最新的(重复的)迁移也被删除;并且迁移运行良好。

    感谢理查德的帮助。

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 2019-06-26
      • 2019-10-04
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      相关资源
      最近更新 更多