【发布时间】:2014-04-30 12:51:55
【问题描述】:
我在 postgreSQL 中使用 JSON 类型。我可以通过添加以下内容来解决本地计算机上的could not identify an equality operator for type json 问题:
execute <<-SPROC
-- This creates a function named hashjson that transforms the
-- json to texts and generates a hash
CREATE OR REPLACE FUNCTION hashjson(
json
) RETURNS INTEGER LANGUAGE SQL STRICT IMMUTABLE AS $$
SELECT hashtext($1::text);
$$;
-- This creates a function named json_eq that checks equality (as text)
CREATE OR REPLACE FUNCTION json_eq(
json,
json
) RETURNS BOOLEAN LANGUAGE SQL STRICT IMMUTABLE AS $$
SELECT bttextcmp($1::text, $2::text) = 0;
$$;
-- This creates an operator from the equality function
CREATE OPERATOR = (
LEFTARG = json,
RIGHTARG = json,
PROCEDURE = json_eq
);
-- Finaly, this defines a new default JSON operator family with the
-- operators and functions we just defined.
CREATE OPERATOR CLASS json_ops
DEFAULT FOR TYPE json USING hash AS
OPERATOR 1 =,
FUNCTION 1 hashjson(json);
SPROC
但是,我在 Heroku 中没有超级用户权限。我该如何解决这个问题?
更新
我使用JSON来存储一个嵌套的hash,记录是这样的:
Company 具有列 properties。属性可以是:
{网站:[{url:1,images:[1,2,3],description:1},{url:2,images:[1,2,3],description:2}],维基百科: {url:1, 描述:2},facebook:{id:1}}
更新 2
错误信息:
PG::UndefinedFunction: 错误: 无法识别 json 类型的相等运算符 第 1 行:SELECT DISTINCT "companies".* FROM "companies" INNER JOIN "reviews...
错误指示箭头位于SELECT DISTINCT。
sql 是由视图中的 this 部分生成的:
@review.companies
在模型中:
class Company < ActiveRecord::Base
store_accessor :properties, [:websites, :fb, :twitter, :linkedin,:country, :city, :street, :postcode]
迁移中:
class AddPropertiesToCompany < ActiveRecord::Migration
def change
add_column :companies, :properties, :json
end
end
再次感谢您的帮助。
【问题讨论】:
-
您试图做什么导致该错误?显示导致错误的 SQL,如果可能,显示相关的 DDL。
-
感谢您的提问。我已经更新了我的问题。我希望这是有道理的。
-
向我们展示导致错误的 Rails 代码可能会有所帮助。
-
对。我再次更新了问题。并发布尽可能多的部分。
-
Ivan 的运气好吗?
标签: ruby-on-rails json postgresql heroku root