2017-07-28 88 views
0

我正在使用Rails 5和PostGres 9.5。我有以下遷移在Rails遷移中向表中添加索引的正確方法是什麼?

class CreateCryptoIndexCurrencies < ActiveRecord::Migration[5.0] 
    def change 
    create_table :crypto_index_currencies do |t| 
     t.references :crypto_currency, foreign_key: true 
     t.date :join_date, :null => false, :default => Time.now 
     t.timestamps 
    end 
    add_index :crypto_index_currencies, :crypto_currency, unique: true 
    end 
end 

在運行遷移,這是死亡與此錯誤

PG::UndefinedColumn: ERROR: column "crypto_currency" does not exist 

什麼是添加索引的正確方法?我想引用的表名稱爲「crypto_currencies」。

回答

0
add_index 'crypto_index_currencies', ['crypto_currency'], name: "index_crypto_index_currencies_on_crypto_currency", unique: true, using: :btree 

內的語法::B樹其可選的。

0

這是添加它使用CREATE_TABLE塊

class CreateCryptoIndexCurrencies < ActiveRecord::Migration[5.0] 
    def change 
    create_table :crypto_index_currencies do |t| 
     t.references :crypto_currency, foreign_key: true 
     t.date :join_date, :null => false, :default => Time.now 
     t.index :crypto_currency, unique: true 
    end 
    end 
end 
相關問題