【问题标题】:Temporary index name too long in Rails migrationRails 迁移中的临时索引名称太长
【发布时间】:2012-01-18 19:12:38
【问题描述】:

我在尝试回滚我的一项迁移时遇到问题。似乎 Rails 正在为迁移生成一个带有临时索引的临时表。我在这张表上的实际索引少于 64 个字符,但是每当 Rails 尝试为其创建临时索引时,它就会变成一个长度超过 64 个字符的名称,并引发错误。

这是我的简单迁移:

class AddColumnNameToPrices < ActiveRecord::Migration
  def self.up
     add_column :prices, :column_name, :decimal
  end

  def self.down
    remove_column :prices, :column_name
  end
end

这是我得到的错误:

==  AddColumnNameToPrices: reverting ============================================
-- remove_column(:prices, :column_name)
rake aborted!
An error has occurred, this and all later migrations canceled:

Index name 'temp_index_altered_prices_on_column_and_other_column_and_third_column' on table     'altered_prices' is too long; the limit is 64 characters

我已经更改了列名,但示例仍然存在。我可以在第二次迁移中进行更改,但这仍然意味着我无法回滚此表上的迁移。我可以在新的迁移中重命名索引,但这仍然使我无法进行单次迁移。

有没有人知道如何解决这个问题?

【问题讨论】:

  • 那是所涉及索引的真实名称吗?
  • 没有没有。只是想从实际项目中抽象一点。

标签: ruby-on-rails ruby-on-rails-3 rails-migrations


【解决方案1】:

今天遇到了这个问题,并通过更改迁移以包括删除和添加导致长名称问题的索引来修复它。这样,在我更改列类型时不会跟踪更改(这是导致真正长名称的地方)

我添加了以下内容:

class FixBadColumnTypeInNotifications < ActiveRecord::Migration
  def change
    # replace string ID with integer so it is Postgres friendly
    remove_index :notifications, ["notifiable_id","notifiable_type"]
    change_column :notifications, :notifiable_id, :integer
    # shortened index name
    add_index "notifications", ["notifiable_id","notifiable_type"], :name => "notifs_on_poly_id_and_type"
  end
 end

【讨论】:

    【解决方案2】:

    看起来您的数据库架构实际上具有名为prices_on_column_and_other_column_and_third_column 的索引。您可能已经在之前的迁移游戏中定义了索引。但不仅仅是从迁移中删除索引定义。

    如果是真的,你有两个选择:

    • 更简单的一种(如果您的代码不在生产环境中,则可以使用)。你可以 使用迁移从头开始重新创建数据库(而不是从 db/schema.rb) 通过调用 rake db:drop db:create db:migrate。确保不要在其他迁移文件中创建具有长名称的索引。如果这样做,请将 :name =&gt; 'short_index_name' 选项添加到 add_index 调用中,以使 rails 为索引生成更短的名称。
    • 如果您在生产数据库上遇到此问题,情况会稍微复杂一些。您可能需要从数据库控制台手动删除索引。

    【讨论】:

    • rake db:migrate:reset 也很有用。
    • 有用,但会删除所有记录(可以理解)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2021-09-06
    • 2012-09-21
    • 2019-02-14
    • 2020-04-10
    • 2019-02-13
    • 2016-12-02
    相关资源
    最近更新 更多