Ruby on rails 重命名“表:关系”;表“U-pkey”;不存在

Ruby on rails 重命名“表:关系”;表“U-pkey”;不存在,ruby-on-rails,ruby-on-rails-4,activerecord,migration,Ruby On Rails,Ruby On Rails 4,Activerecord,Migration,Rails4.1中一个非常简单的迁移失败了,我不明白为什么。错误: PG::UndefinedTable: ERROR: relation "channel_entries_pkey" does not exist: ALTER INDEX "channel_entries_pkey" RENAME TO "channels_projects_pkey" 以下是迁移: class ConvertChannelEntriesToChannelsProjects < ActiveRecor

Rails4.1中一个非常简单的迁移失败了,我不明白为什么。错误:

PG::UndefinedTable: ERROR:  relation "channel_entries_pkey" does not exist:
ALTER INDEX "channel_entries_pkey" RENAME TO "channels_projects_pkey"
以下是迁移:

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
class ConvertChannelEntriestoChannelsProject
一点背景:频道和项目之间的连接是一个HM-THRU命名的频道\ U条目,以容纳额外的
位置
。由于这个位置已经被取消,我将切换到一个简单的HABTM,按照惯例,连接表现在应该命名为channels\u projects


我记得,
\u pkey
索引是为
外键
s自动创建的,但频道项上从来没有外键。为什么
rename\u table
要重命名这个不存在的索引?

解决了这个问题,一个旧的迁移没有完全工作,并且留下了一个未命名的约束。

为类似的问题添加了我的解决方案。 下面是运行迁移时的错误

01 PG::UndefinedTable: ERROR:  relation "fundraise_stories_pkey" does not exist
01 : ALTER INDEX "fundraise_stories_pkey" RENAME TO "fundraisers_pkey"
通过pgadmin和表fundraise_故事的选定约束连接到数据库。它的名字是“fundrise_stories_pkey”。 所以这是一个老错误,因为约束名与表名不匹配

解决方案:

  • 在表的约束部分下查找现有索引名。就我而言,这是“筹款故事”
  • 在重命名表之前重命名索引
  • 最后,重命名该表 下面是重命名表之前重命名索引的修改迁移

    def self.up
        execute "ALTER INDEX fundrise_stories_pkey RENAME TO fundraise_stories_pkey;"
        rename_table :fundraise_stories, :fundraisers
    end
    
    日志

    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) ==