Index name too long attachinary/postgresql

时间:2017-06-14 21:41:06

标签: sql ruby-on-rails ruby postgresql activerecord

I keep getting this error after installing the ruby gem for Attachinary and trying to run db:migrate:

Index name 'index_attachinary_files_on_attachinariable_type_and_attachinariable_id' on table 'attachinary_files' is too long; the limit is 63 characters

I've been searching and searching for a solution and of course heard about giving a name to the index to avoid the generated one but it doesn't seem to be working. There is actually a name field already that says:

'name: by_scoped_parent'

Here is the full line in my migrate file:

add_index :attachinary_files, [:attachinariable_type, :attachinariable_id, :scope], name: 'by_scoped_parent'

Here is the content of the migration file:

class CreateAttachinaryTables < ActiveRecord::Migration[5.1]
  def change
    create_table :attachinary_files do |t|
      t.references :attachinariable, polymorphic: true
      t.string :scope

      t.string :public_id
      t.string :version
      t.integer :width
      t.integer :height
      t.string :format
      t.string :resource_type
      t.timestamps
    end

    add_index :attachinary_files, [:attachinariable_type, :attachinariable_id, :scope], name: 'by_scoped_parent'
  end
end

Note: Don't have any doublon in my db/migrate folder. The last migration files went perfectly well.

I'm currently learning Rails so I hope someone can help and forgive me if it's a silly question .

Thanks in advance!

1 个答案:

答案 0 :(得分:2)

由于迁移中的这一行,您收到错误:

t.references :attachinariable, polymorphic: true

默认索引为true。如果你不想要这个索引(这似乎就是你在索引中稍后添加作用域的情况),那么make index:false:

t.references :attachinariable, polymorphic: true, index: false

或添加名称:

t.references :attachinariable, polymorphic: true, index: {name: 'name of your choice here'}