Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't rename an ActiveRecord table a second time. "[table]_pkey" does not exist

Some time ago, I ran the following rails migration to change the name of a table:

class RenameMaterialDonationsToMaterialDonationRequests < ActiveRecord::Migration
  def change
    rename_table :material_donations, :material_donation_requests
  end
end

Now I need to rename the table again. Here's my migration:

class RenameMaterialDonationRequestsToHelpRequests < ActiveRecord::Migration
  def change
    rename_table :material_donation_requests, :help_requests
  end
end

However, when I run the migration, I'm getting the following error:

PG::Error: ERROR:  relation "material_donation_requests_pkey" does not exist
: ALTER INDEX "material_donation_requests_pkey" RENAME TO "help_requests_pkey"/Users/[me]/.rvm/gems/ruby-2.1.4/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `exec'

I'm using Postgresql. In pgAdmin3, I can see that the pkey still retains the table name from before the first migration:

CONSTRAINT material_donations_pkey PRIMARY KEY (id)

How can I fix this to rename the table?

like image 621
tobogranyte Avatar asked Dec 12 '15 16:12

tobogranyte


1 Answers

The answer below might work, but I decided to go with this:

execute "ALTER INDEX material_donations_pkey RENAME TO material_donation_requests_pkey;"

I chose this because it is the command that the migration was trying to run automatically as part of the original migration. That command wasn't automatically part of the pre-4.0 rails when I renamed this table the first time so I ran it now. I felt more comfortable doing exactly what rails is doing presently.

like image 164
tobogranyte Avatar answered Sep 27 '22 19:09

tobogranyte