Skip to content
This repository was archived by the owner on May 13, 2022. It is now read-only.
This repository was archived by the owner on May 13, 2022. It is now read-only.

Can't rollback change_column #169

@zedalaye

Description

@zedalaye

I coded this migration :

class ChangeForeignKeyConstraints < ActiveRecord::Migration

  def self.up
    change_column :profiles, :company_id, :integer, foreign_key: { references: :companies, on_update: :cascade, on_delete: :cascade }
    [ :addresses, :web_links, :distributors, :customers ].each do |table|
      change_column table, :profile_id, :integer, foreign_key: { references: :profiles, on_update: :cascade, on_delete: :cascade }
    end
  end

  def self.down
    [ :addresses, :web_links, :distributors, :customers ].reverse.each do |table|
      change_column table, :profile_id, :integer, foreign_key: { references: :profiles, on_update: :no_action, on_delete: :no_action }
    end
    change_column :profiles, :company_id, :integer, foreign_key: { references: :companies, on_update: :no_action, on_delete: :no_action }
  end

end

What's strange is that the "up" migration works while "down" fails with this message :

StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERREUR:  the relation « fk__customers_profile_id » already exists: 
CREATE INDEX "fk__customers_profile_id" ON "customers" ("profile_id")

Please note that even for the "up" migration, the foreign key ALREADY exists. I just wanted to alter on_delete and on_update to :cascade (and revert them to :no_action on rollback)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions