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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With