2017-06-14 79 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 :) –