3

既存の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
4

1 に答える 1

7

おそらくそれはあなたの場合の単純なデータ型の問題です、試してみてください:

data @> ('complete'::text => 'true'::text)

ただし、=>演​​算子は非推奨になりました(9.2で削除されました)。次のhstore()形式を使用することをお勧めします。

(data @> hstore('complete','true'))
于 2013-01-26T10:23:34.213 に答える