Foreign Key in Rails - Errors

Viewed 1054

I'm trying to add a new foreign key column to my Customers table. This is my migration:

class AddCompanyForeignKeyToCustomers < ActiveRecord::Migration[5.1]
  def change
    add_reference :customers, :company, foreign_key: true
    add_foreign_key :customers, :companies
  end
end

These are the errors I'm getting:

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

PG::DuplicateObject: ERROR:  constraint "fk_rails_ef51a916ef" for relation "customers" already exists
: ALTER TABLE "customers" ADD CONSTRAINT "fk_rails_ef51a916ef"
FOREIGN KEY ("company_id")
  REFERENCES "companies" ("id")

ActiveRecord::StatementInvalid: PG::DuplicateObject: ERROR:  constraint "fk_rails_ef51a916ef" for relation "customers" already exists
: ALTER TABLE "customers" ADD CONSTRAINT "fk_rails_ef51a916ef"
FOREIGN KEY ("company_id")
  REFERENCES "companies" ("id")

PG::DuplicateObject: ERROR:  constraint "fk_rails_ef51a916ef" for relation "customers" already exists

What does any of that mean? I don't know what fk_rails_$NUMBER is.

2 Answers
Related