gpt4 book ai didi

ruby-on-rails - Rails 3.1、Postgres 9.1.4、hstore 未知运算符错误

转载 作者:行者123 更新时间:2023-11-29 11:39:47 24 4
gpt4 key购买 nike

我正在尝试将 hstore 数据列添加到现有 STI 模型。

类似于Postgres HStore Errors - Unknown Operator但据我所知,我已经安装了 hstore 扩展。我已经删除了数据库并从迁移中重建它而没有错误,但规范仍然失败。

Mac OS X 10.8.2
Rails 3.1.10
psql (PostgreSQL) 9.1.4

用户.rb

has_many :targets

def complete_targets
targets.where("data @> (:key => :value)", key: 'complete', value: 'true')
end

目标.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

最佳答案

也许你的情况是一个简单的数据类型问题,试试:

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

但是 => 运算符已被弃用(并已在 9.2 中删除),最好使用 hstore() 格式:

(data @> hstore('complete','true'))

关于ruby-on-rails - Rails 3.1、Postgres 9.1.4、hstore 未知运算符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14535874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com