Heroku rake aborted - 如何清理我的迁移文件?

时间:2015-11-18 20:42:09

标签: ruby-on-rails-4 heroku rake

我正在使用Rails应用程序。它在本地工作正常,但在将其推送到Heroku并运行heroku run rake db:migrate之后,取消外键时取消了rake:产品在表格中:订阅。 我知道我不应该为这些迁移获得奖章;但它仍然适用于我的本地版本。

我不确定如何解决它(可能会为某些迁移文件添加down方法?),因为这是我有史以来的第一个Rails项目......

以下是日志文件中提到的一些迁移文件,包括20150929194006。

20150926163647_create_subscriptions:

class CreateSubscriptions < ActiveRecord::Migration
def change
create_table :subscriptions do |t|
  t.references :project, index: true
  t.references :user, index: true

  t.timestamps null: false
end
add_foreign_key :subscriptions, :projects
add_foreign_key :subscriptions, :users

add_index :subscriptions, [:project_id, :user_id], :unique => true
end
end

20150929192517_add_field_to_subscriptions

class AddFieldToSubscriptions < ActiveRecord::Migration
def change
add_reference :subscriptions, :product
add_foreign_key :subscriptions, :products
end
end

20150929194006_remove_product_from_subscriptions

class RemoveProductFromSubscriptions < ActiveRecord::Migration
def change
remove_reference :subscriptions, :product, index: true
remove_foreign_key :subscriptions, :products
end
end

20150929194239_add_product_to_subscriptions

class AddProductToSubscriptions < ActiveRecord::Migration
def change
add_reference :subscriptions, :product
add_foreign_key :subscriptions, :products
add_index :subscriptions, :product_id, :unique => true
end
end

20150930073055_remove_index_from_subscriptions

class RemoveIndexFromSubscriptions < ActiveRecord::Migration
def change
remove_index :subscriptions, :product_id
add_index :subscriptions, [:product_id, :user_id], unique: true
end
end

错误日志:

Running rake db:migrate on murmuring-cove-7571... up, run.2130
  ActiveRecord::SchemaMigration Load (1.9ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to RemoveProductFromSubscriptions (20150929194006)
   (1.8ms)  BEGIN
== 20150929194006 RemoveProductFromSubscriptions: migrating ===================
-- remove_reference(:subscriptions, :product, {:index=>true})
   (4.9ms)  ALTER TABLE "subscriptions" DROP "product_id"
   -> 0.0052s
-- remove_foreign_key(:subscriptions, :products)
   (5.5ms)  SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete
FROM pg_constraint c
JOIN pg_class t1 ON c.conrelid = t1.oid
JOIN pg_class t2 ON c.confrelid = t2.oid
JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
JOIN pg_namespace t3 ON c.connamespace = t3.oid
WHERE c.contype = 'f'
  AND t1.relname = 'subscriptions'
  AND t3.nspname = ANY (current_schemas(false))
ORDER BY c.conname

   (1.7ms)  ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Table 'subscriptions' has no foreign key on column 'product_id'/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:756:in `block in remove_foreign_key'

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:0)

我能够解决它。似乎PostGres处理迁移文件的方式与本地sqlite解决方案不同。有一些不必要的迁移文件(添加了以后删除的外键)。

删除两个不必要的文件,提交更改(git commit -m""),然后运行git push heroku master,然后运行heroku run rake db:migrate,一切运行顺利!