Reputation: 3454
A really simple migration in Rails 4.1 fails and I don't see why. The error:
PG::UndefinedTable: ERROR: relation "channel_entries_pkey" does not exist:
ALTER INDEX "channel_entries_pkey" RENAME TO "channels_projects_pkey"
Here's the migration:
class ConvertChannelEntriesToChannelsProjects < ActiveRecord::Migration
def up
remove_column :channel_entries, :position
rename_table :channel_entries, :channels_projects
end
def down
rename_table :channels_projects, :channel_entries
add_column :channel_entries, :position, :integer
end
end
A little background: The join between channels and projects has been a HM-THRU named channel_entries to accommodate an additional position
. Since the position has been dropped, I'm switching to a simple HABTM and by convention, the join table should now be named channels_projects.
As I recall, _pkey
indexes are automatically created for foreign_key
s, but no foreign keys have ever existed on channel_entries. Why does rename_table
want to rename this non-existant index?
Upvotes: 2
Views: 1489
Reputation: 4436
Adding my solution for similar problem. Below is the error while running migrations
01 PG::UndefinedTable: ERROR: relation "fundraise_stories_pkey" does not exist
01 : ALTER INDEX "fundraise_stories_pkey" RENAME TO "fundraisers_pkey"
Connected to database via pgadmin and selected constraints for the table fundraise_stories. It was showing constraint name as "fundrise_stories_pkey". So it was some old mistake because constraint name doesnt match with table name.
Solution:
Below is modified migration to rename index before renaming table.
def self.up
execute "ALTER INDEX fundrise_stories_pkey RENAME TO fundraise_stories_pkey;"
rename_table :fundraise_stories, :fundraisers
end
Log
D, [2020-02-02T17:16:27.428294 #7363] DEBUG -- : (0.2ms) BEGIN
== 20200127102616 RenameFundraiseStoryTableToFundraisers: migrating ===========
-- execute("ALTER INDEX fundrise_stories_pkey RENAME TO fundraise_stories_pkey;")
D, [2020-02-02T17:16:27.434366 #7363] DEBUG -- : (5.5ms) ALTER INDEX fundrise_stories_pkey RENAME TO fundraise_stories_pkey;
-> 0.0061s
-- rename_table(:fundraise_stories, :fundraisers)
D, [2020-02-02T17:16:27.435722 #7363] DEBUG -- : (0.7ms) ALTER TABLE "fundraise_stories" RENAME TO "fundraisers"
D, [2020-02-02T17:16:27.438769 #7363] DEBUG -- : (0.3ms) ALTER TABLE "public"."fundraise_stories_id_seq" RENAME TO "fundraisers_id_seq"
D, [2020-02-02T17:16:27.439334 #7363] DEBUG -- : (0.2ms) ALTER INDEX "fundraise_stories_pkey" RENAME TO "fundraisers_pkey"
D, [2020-02-02T17:16:27.445452 #7363] DEBUG -- : (0.8ms) ALTER INDEX "index_fundraise_stories_on_bank_account_id" RENAME TO "index_fundraisers_on_bank_account_id"
D, [2020-02-02T17:16:27.446153 #7363] DEBUG -- : (0.3ms) ALTER INDEX "index_fundraise_stories_on_creator_id_and_creator_type" RENAME TO "index_fundraisers_on_creator_id_and_creator_type"
-> 0.0131s
== 20200127102616 RenameFundraiseStoryTableToFundraisers: migrated (0.0193s) ==
Upvotes: 1
Reputation: 3454
Figured it out, an old migration didn't fully work and left a constraint unrenamed.
Upvotes: 2