9

如何按 hstore 属性对查询结果进行排序?

@items = Item.includes(:product).order('products.properties @> hstore("platform")')

原因

PG::Error: ERROR:  column "platform" does not exist
LINE 1: ...oduct_id"  ORDER BY products.properties @> hstore("platform"...

platform是一个hstore key,存放在properties列中,是一个hstore类型。

4

1 回答 1

17

双引号用于在 PostgreSQL(和其他遵循标准的数据库)中引用标识符(例如表和列名)。所以当你说:

hstore("platform")

PostgreSQL 将"platform"其视为带引号的列名,并且由于没有platform列,因此您会收到错误消息。

标准 SQL 中的字符串用单引号引起来,你想说:

.order("products.properties @> hstore('platform')")

不过,这可能仍然会失败,hstore('platform')没有多大意义,也没有@>在这里使用;a @> b方法

hstore 是否a 包含 hstoreb

如果您尝试对 hstore 中'platform'键的值进行排序,properties那么您希望使用如下->方式查找'platform'键:

.order("products.properties -> 'platform'")
于 2013-04-29T18:11:03.683 回答