2017-06-14 40 views
1

我继续安装红宝石创业板Attachinary并试图运行db:migrate后收到此错误:指标名称太长attachinary/PostgreSQL的

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

我一直在寻找,寻找一个解决方案,当然听说过给索引命名以避免生成的索引,但它似乎不起作用。其实有一个名称字段已经写着:

'name: by_scoped_parent' 

这里是我的迁移文件的完整行:

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

下面是迁移文件的内容:

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 

注意:在我的db/migrate文件夹中没有任何双倍数据。最后的迁移文件非常好。

我目前正在学习Rails,所以我希望有人能帮助并原谅我,如果这是一个愚蠢的问题。

在此先感谢!

+0

您是否生成过两次迁移? –

+0

嘿@AlejandroMontilla!它实际上这里迁移 –

+0

是我所得到的终端后,轨道DB之前停止:迁移--trace: “**调用分贝:迁移(FIRST_TIME) **调用环境(FIRST_TIME) **执行环境 * *调用数据库:load_config(first_time) **执行数据库:load_config **执行数据库:迁移 == 20170614155451 CreateAttachinaryTables:迁移===================== ===== - create_table(:attachinary_files) rails中止! StandardError:发生错误,此次及以后所有迁移取消: 表'attachinary_files'上的索引名称'index_attachinary_files_on_attachinariable_type_and_attachinariable_id'太长..' –

回答

2

你所得到的错误,由于该线路迁移:

t.references :attachinariable, polymorphic: true 

默认情况下,指数是真实的。如果你不希望这个指数(这好像是你在指数后面添加范围也如此),则使指数:假:

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

或者添加一个名字:

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

嗨Archana!非常感谢,非常感谢。你能告诉我更多关于我的问题吗?我在网上做了一些研究,但没有发现任何人有这个问题(与这个特殊的宝石有关)。该文件是在输入'rails attachinary:install:migrations'后生成的,所以我没有真正选择任何东西。为什么会发生在我身上?另外,“add_index”行中的“name”参数不应该是索引名称?无论如何,再次感谢! –

+0

@ThomasCailhol每当在移植rails中有一个'reference'(一个外键)默认创建一个索引。外键总是应该有索引。在多态关联的情况下,它会尝试在id和type列上创建索引,并且在您的情况下它们都结合得太长。我希望这能解释这个问题。 – archana

+0

非常感谢@archana :) –