Django and Postgis: How can I create a spatial table with CONSTRAINT enforce_geotype_geom in Django?

StackOverflow https://stackoverflow.com/questions/23331616

Question

I'm trying to create a spatial table like this(from here):

CREATE TABLE landmarks
(
  id serial NOT NULL,
  name character varying(50),
  the_geom geometry,
  CONSTRAINT landmarks_pkey PRIMARY KEY (gid),
  CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2),
  CONSTRAINT enforce_geotype_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL),
  CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326)
);

How can I create a table like this in Django model? I know how to create the fields but dont know how to add the CONSTRAINT.

Thanks very much!!

Was it helpful?

Solution

The old PostGIS model before 2.x used constraints like in the question. The newer style uses typmods like geometry(Point, 4326).

The old style can set up manually using the appropriate DDL, or with the utility function AddGeometryColumn with use_typmod=false.

The newer typmod style is typically recommended over the older constraints-based style.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top