Rails Postgres功能索引 [英] Rails Postgres functional indexes
问题描述
我应该如何输入包含函数的multicolum索引到schema.rb?
How I should enter my multicolum indexes which contain functions into schema.rb ?
例如这不起作用:
add_index "temporary_events", ["templateinfoid", "campaign", "date(gw_out_time)", "messagetype"], :name => "temporary_events_campaign_tinfoid_date_messagetype"
rake db:test:load
rake db:test:load
rake aborted!
rake aborted!
PGError:ERROR:列date(gw_out_time)不存在
PGError: ERROR: column "date(gw_out_time)" does not exist
:CREATE INDEXtemporary_events_campaign_tinfoid_date_messagetypeONtemporary_events(templateinfoid,campaign,date(gw_out_time,messagetype)
: CREATE INDEX "temporary_events_campaign_tinfoid_date_messagetype" ON "temporary_events" ("templateinfoid", "campaign", "date(gw_out_time", "messagetype")
推荐答案
用于创建索引的内置ActiveRecord方法( add_index
)不支持函数或任何其他更高级的功能。您可以使用执行
来创建带有SQL的索引:
The built-in ActiveRecord method for creating indexes (add_index
) does not support functions or any other more advanced features. Instead you can use execute
to create the index with SQL:
execute <<-SQL
CREATE INDEX temporary_events_campaign_tinfoid_date_messagetype
ON temporary_events(templateinfoid, campaign, date(gw_out_time), messagetype);
SQL
请注意在m中使用执行
如果您不使用SQL模式格式( config.active_record.schema_format =:sql
),则igrations可能会出现问题。有关详细信息,请搜索 schema_format 。
Note that the use of execute
in migrations can be problematic if you are not using the SQL schema format (config.active_record.schema_format = :sql
). For more information, search for schema_format.
这篇关于Rails Postgres功能索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!