在 Rails 迁移中向 table 添加索引的正确方法是什么?
What's the proper way to add an index to a table in a Rails migration?
我正在使用 Rails 5 和 PostGres 9.5。我有以下迁移
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
添加索引的正确方法是什么?我要引用的 table 名称称为 "crypto_currencies".
这是将其添加到 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
add_index 'crypto_index_currencies', ['crypto_currency'], name: "index_crypto_index_currencies_on_crypto_currency", unique: true, using: :btree
使用::btree 它是可选的。
我正在使用 Rails 5 和 PostGres 9.5。我有以下迁移
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
添加索引的正确方法是什么?我要引用的 table 名称称为 "crypto_currencies".
这是将其添加到 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
add_index 'crypto_index_currencies', ['crypto_currency'], name: "index_crypto_index_currencies_on_crypto_currency", unique: true, using: :btree
使用::btree 它是可选的。