1

I try to add a constraint based on geometry type to a "street" table that prevents anything except Linestring geometry or Null.

alter table streets
add constraint streets_geom_linestring_chk
check(st_geometrytype(geom) = 'ST_LINESTRING'::text OR geom IS NULL);

When I try to store geometries in the table:

update streets set geom=st_geomfromtext('LINESTRING(0 8,12 8)') where id=1;

I got an error, that the new row for relation violates check constraint.

  • 1
    Please edit your question and add a more descriptive title. Titles should be written as questions. – underdark Feb 24 '14 at 15:41
3

ST_GeometryType returns 'ST_Linestring', 'ST_Polygon','ST_MultiPolygon' etc. You can change check to use GeometryType which return mentioned values without ST_ prefix

  • Yes of course, the solution is the st_ prefix in the geometrytype function. I had already tried to remove the st_ in linestring and add a st_ at the input. sometimes the solution is so simple. many thanks for the quick response. – surfer_cid Feb 24 '14 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.