在PostgreSQL 9.1中更改列的数据类型

时间:2013-01-01 21:49:59

标签: postgis postgresql-9.1 type-conversion

我正在通过“PostGIS in Action”一书学习PostgreSQL和PostGIS。我的问题来自Pg。清单3.4中的66。

我的代码如下:

CREATE TABLE ch03.paris_polygons(tags hstore, CONSTRAINT paris_polygons_pk PRIMARY KEY (gid)
)
INHERITS (ch03.paris);

ALTER TABLE ch03.paris_polygons NO INHERIT ch03.paris;
INSERT INTO ch03.paris_polygons(osm_id, ar_num, geom, tags, feature_name, feature_type)
SELECT osm_id, ar_num, ST_Multi(geom) As geom, tags, tags->'name',
COALESCE(tags->'tourism', tags->'railway','other')::varchar(50) As feature_type
FROM ch03.paris_hetero
WHERE ST_GeometryType(geom) LIKE '%Polygon';
SELECT populate_geometry_columns('ch03.paris_polygons'::regclass);
ALTER TABLE ch03.paris_polygons INHERIT ch03.paris;

运行代码后收到此错误:

ERROR: child table "paris_polygons" has different type for column "geom"
SQL state: 42804

我用Google搜索了这个:

-204(ECPG_INT_FORMAT)

The host variable is of type int and the datum in the database is of a different type and contains a value that cannot be interpreted as an int. The library uses strtol() for this conversion. (SQLSTATE 42804)

psql命令会帮助我知道如何合并这些命令?

再次感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

我刚碰到这个。这本书很可能在你(和我)看到的代码片段中忘记了这一步。但是,如果您继续阅读下一页,它们只显示了LINESTRING分区表的构造,那里有一条关键线,它们在POLYGON分区表结构中忘记了:

ALTER TABLE ch03.paris_polygons
ADD CONSTRAINT enforce_geotype_geom
CHECK (geometrytype(geom) = 'POLYGON'::TEXT);

一旦我添加了该约束,错误消失了,基本上它导致的是允许geom列继承父geom列类型,而不是它假定的几何('polygon',SRID)类型基于数据加载,并留下约束来管理类型限制。

答案 1 :(得分:0)

我遇到了同样的错误。我尝试使用' MULTIPOLYGON'代替:

ALTER TABLE ch03.paris_polygons
ADD CONSTRAINT enforce_geotype_geom
CHECK (geometrytype(geom) = 'MULTIPOLYGON'::TEXT);