I'm working on a Rails app.It works fine locally, but after pushing it to Heroku and running heroku run rake db:migrate
the rake is cancelled on the removal of the foreign key for :product in the tabel :subscriptions.
I know that I don't deserve a medal for those migrations; but it still does work on my Local version.
I'm not sure how to solve it (maybe adding down-methods to some of the migration files?), since this is my first Rails project ever...
Here are some of the migration files around the one that is mentioned in the log-files, including 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
Error logs:
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'
Any ideas on how I can solve this?
I was able to solve it. It seems that PostGres handles the migration-files differently then the local sqlite solution. There were some unnecessary migration-files (added foreign keys that where deleted later on).
By deleting both unnecessary files, committing the change (git commit -m""
) and then run a git push heroku master
and afterwards a heroku run rake db:migrate
, everything runs smooth!