【问题标题】:Ruby on Rails adding another index through migration results in over 64 character limitRuby on Rails 通过迁移添加另一个索引导致超过 64 个字符的限制
【发布时间】:2013-02-23 03:10:11
【问题描述】:

我想在我的索引表中添加另一个索引列,但是收到的表索引太长;限制为 64 个字符。顺便说一下 Rails 3 和 MySQL DB。

当前架构如下:

  create_table "admin_users_projects", :force => true do |t|
    t.integer "admin_user_id"
    t.integer "project_id"
  end

  add_index "admin_users_projects", ["admin_user_id", "project_id"], :name =>     "index_admin_users_projects_on_admin_user_id_and_project_id"

我正在尝试运行以下迁移以添加索引:

class AddIndexToAdminUsersProjects < ActiveRecord::Migration
  def change
    add_index :index_admin_users_projects_on_admin_user_id_and_project_id,     :admin_users_project_id
  end
end

但是在尝试 rake 时得到以下信息(简而言之):

Larrys-MacBook-Pro:scrumtool larrydavid$ rake db:migrate
==  AddIndexToAdminUsersProjects: migrating ===================================
-- add_index(:index_admin_users_projects_on_admin_user_id_and_project_id,     :admin_users_project_id)
rake aborted!
An error has occurred, all later migrations canceled:

Index name     'index_index_admin_users_projects_on_admin_user_id_and_project_id_on_admin_users_project_id'     on table 'index_admin_users_projects_on_admin_user_id_and_project_id' is too long; the limit     is 64 characters
[...]

【问题讨论】:

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


    【解决方案1】:

    试试这个

    class AddIndexToAdminUsersProjects < ActiveRecord::Migration
      def change
        add_index :admin_users_project, [:admin_users_id, :project_id], :name => 'index_admin_projects_on_admin_and_project'
      end
    end
    

    【讨论】:

    • 完全正确,您必须自己指定名称。但请记住,如果您想在之后通过迁移删除索引,您还必须在此处指定名称(或者,否则,创建一个基于列以编程方式查找索引的方法,然后删除它们)。跨度>
    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    相关资源
    最近更新 更多