既存のSTIモデルにhstoreデータ列を追加しようとしています。
Postgres HStoreエラーに似ています-不明な演算子ですが、私が知る限り、hstore拡張機能をインストールしました。データベースを削除し、移行からエラーなしで再構築しましたが、仕様はまだ失敗します。
Mac OS X 10.8.2
Rails 3.1.10
psql (PostgreSQL) 9.1.4
user.rb
has_many :targets
def complete_targets
targets.where("data @> (:key => :value)", key: 'complete', value: 'true')
end
targets.rb
belongs_to :user
serialize :data, ActiveRecord::Coders::Hstore
setup_hstoreの移行:
class SetupHstore < ActiveRecord::Migration
def self.up
execute "CREATE EXTENSION IF NOT EXISTS hstore"
end
def self.down
execute "DROP EXTENSION IF EXISTS hstore"
end
end
データ列の移行を追加します。
class AddDataToRecords < ActiveRecord::Migration
def change
add_column :records, :data, :hstore
end
end
インデックスの移行を追加します。
class AddHstoreIndexes < ActiveRecord::Migration
def up
execute "CREATE INDEX records_gin_data ON records USING GIN(data)"
end
def down
execute "DROP INDEX records_gin_data"
end
end
エラー:
ActiveRecord::StatementInvalid:
PG::Error: ERROR: operator does not exist: unknown => unknown
LINE 1: ...records"."user_id" = 244 AND (data @> ('complete' => 'true')...
直接クエリを介して拡張機能を作成しようとしたときのNavicat出力:
CREATE EXTENSION hstore Error : ERROR: extension "hstore" already exists