如何修复重复的迁移名称错误

时间:2014-10-17 17:55:15

标签: ruby-on-rails heroku

我一直在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)

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

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

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

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)

我现在需要重新添加旧迁移吗?其中一个迁移比另一个迁移得最近,我应该将原始迁移添加回我的程序文件并再次提交吗?

2 个答案:

答案 0 :(得分: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并再次迁移。

答案 1 :(得分:0)

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

感谢Richard的帮助。