【问题标题】:Heroku error with PostgreSQL "could not identify an equality operator for type json"PostgreSQL 的 Heroku 错误“无法识别 json 类型的相等运算符”
【发布时间】: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


【解决方案1】:

一种解决方案是使用 hstore 代替 json 字段类型。这样做的问题是将现有的 json 转换为 hstore。

另一种解决方案是在查询中使用 GROUP BY,而不是 SELECT DISTINCT。

但在某些情况下,将字段类型更改为 hstore 会更容易,例如在使用活动管理员时。

我的一个模型中有一个 HABTM 关联,​​活跃的管理员会尝试查询:

db_dev=# SELECT DISTINCT "api_v1_test".* FROM "api_v1_test" INNER JOIN "api_v1_test_users" ON "api_v1_test"."id" = "api_v1_test_users"."test_id" WHERE "api_v1_test_users"."user_id" = 200;
ERROR:  could not identify an equality operator for type json
LINE 1: SELECT DISTINCT "api_v1_test".* FROM "api_v1_test" INNER JOI...

不想花太多时间解决这个问题并弄清楚如何重写这些查询,我只是将字段转换为 hstore,现在一切正常。

另外,你不能从 heroku 进入 psql 控制台来创建你的函数吗?或者为他们创建一个新的迁移?

【讨论】:

  • 谢谢。 Heroku 说我没有特权。你有使用 Heroku 的经验吗?
  • @StuR 如果不能转换为 hstore 怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
相关资源
最近更新 更多