Pergunta

I'm trying to change my polygon column type to a multipolygon column type.

My code is a simple line.

change_column :messages, :area_shape, :multipolygon, srid: 3785

But Postgres doesn't know this type. I thought that I missed something on my PostGIS configuration but I can't see it.

This my exact error:

rake aborted!
PG::UndefinedObject: ERROR:  type "multipolygon" does not exist
: ALTER TABLE "messages" ALTER COLUMN "area_shape" TYPE multipolygon

This is how I've created my area_shape as a polygon type:

add_column :messages, :area_shape, :polygon, srid: 3785

Thank you for your help.

Foi útil?

Solução 2

Finally I had to remove then recreate my column:

remove_column :messages, :area_shape
add_column :messages, :area_shape, :multi_polygon, srid: 3785

I think I can now understand why it's not possible. Indeed, it seems difficult to change a polygon type to a multi_polygon type without losing data logique.

If you really need to change the type, you can use what was said by @Mike (manually) and create a small method to convert polygon to multi_polygon but it's not really safe in my mind. Tip: a multi_polygon type is an Enumerable that means multi_polygon accepts Array type.

Outras dicas

I have no idea how RGeo attempts to implement change_column (is there documentation for it?), but it isn't correct since there is no such multipolygon type.

If you have direct access to PostgreSQL, following from this answer, use this DDL:

ALTER TABLE my_table
  ALTER COLUMN area_shape TYPE geometry(MultiPolygon,3785)
    USING ST_Multi(area_shape);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top