PG :: UndefinedFunction:ERROR:运算符不存在:geometry&&框

时间:2013-12-10 01:29:06

标签: postgresql types operators postgis

为什么PostgreSQL会抱怨&&运算符不存在? (我安装了PostGIS - 见下文)。

mydb=# SELECT "monuments".* FROM "monuments" WHERE
mydb=# (coord && '-10,-10,10,10'::box)
mydb=# ORDER BY created_at DESC ;
ERROR:  operator does not exist: geometry && box
LINE 1: ...LECT "monuments".* FROM "monuments" WHERE (coord && '-10...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

我安装了PostGIS:

mydb=# select postgis_full_version();
NOTICE:  Function postgis_topology_scripts_installed() not found. Is topology support enabled and topology.sql installed?
                                                                      postgis_full_version                                                                      
----------------------------------------------------------------------------------------------------------------------------------------------------------------
 POSTGIS="2.1.0 r11822" GEOS="3.3.8-CAPI-1.7.8" PROJ="Rel. 4.8.0, 6 March 2012" GDAL="GDAL 1.10.0, released 2013/04/24" LIBXML="2.9.1" LIBJSON="UNKNOWN" RASTER

顺便说一下,我的桌子看起来像这样:

mydb=# \d monuments
 id    | integer              | not null default nextval('monuments_id_seq'::regclass)
 coord | geometry(Point,3785) |

如果您需要更多信息,请与我们联系。

2 个答案:

答案 0 :(得分:3)

boxbuilt-in PostgreSQL primitive geometric type,与point类似。

postgres=> \dT box
                      List of data types
   Schema   | Name |               Description                
------------+------+------------------------------------------
 pg_catalog | box  | geometric box '(lower left,upper right)'
(1 row)

PostGIS使用its own geometry类型,并且通常不会与PostgreSQL内置的基本几何类型良好地互操作。这些是我在PostgreSQL 9.3安装中使用PostGIS 2 &&支持的数据类型组合:

postgres=# \do &&
                                 List of operators
   Schema   | Name | Left arg type | Right arg type | Result type |   Description   
------------+------+---------------+----------------+-------------+-----------------
 pg_catalog | &&   | anyarray      | anyarray       | boolean     | overlaps
 pg_catalog | &&   | anyrange      | anyrange       | boolean     | overlaps
 pg_catalog | &&   | box           | box            | boolean     | overlaps
 pg_catalog | &&   | circle        | circle         | boolean     | overlaps
 pg_catalog | &&   | polygon       | polygon        | boolean     | overlaps
 pg_catalog | &&   | tinterval     | tinterval      | boolean     | overlaps
 pg_catalog | &&   | tsquery       | tsquery        | tsquery     | AND-concatenate
 public     | &&   | geography     | geography      | boolean     | 
 public     | &&   | geometry      | geometry       | boolean     | 
 public     | &&   | geometry      | raster         | boolean     | 
 public     | &&   | raster        | geometry       | boolean     | 
 public     | &&   | raster        | raster         | boolean     | 
(12 rows)

您会看到box支持box && box,但box && geometry不支持coord。由于您的geometry列属于box类型,因此您需要将geometry转换为geometry && geometry,以便最终得到WHERE (coord && geometry(polygon('((-10, -10), (10, 10))'::box)))

示例:

{{1}}

答案 1 :(得分:1)

最简单的解释是您将扩展程序安装到当前search_path以外的某个架构中。

您知道吗,您甚至可以"schema-qualify" operators?像:

SELECT 3 OPERATOR(pg_catalog.+) 4;

或者:

SELECT * FROM public.monuments
WHERE  coord OPERATOR(my_postgis_schema.&&) '-10,-10,10,10'::box);

这样,您可以使查询独立于当前search_path。但更好的是,修复它。