1

I have the following table and sequence in my postgresql-8.4 database:

CREATE TABLE complexobjectpy
(
  id integer NOT NULL,
  the_geom geometry,
  semcat integer,
  CONSTRAINT complexobjectpy_pkey PRIMARY KEY (id),
  CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2),
  CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 900913)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE complexobjectpy OWNER TO tss;

CREATE SEQUENCE seq_complexobjectpy
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;
ALTER TABLE seq_complexobjectpy OWNER TO tss;

The following is instead my Django model:

class Complexobjectpy(models.Model):
    the_geom = models.GeometryField(srid=900913)
    semcat = models.IntegerField()
    objects = models.GeoManager()
    class Meta:
        db_table = u'complexobjectpy'

Performing the following query:

myObj = Complexobjectpy(semcat=50, the_geom=geometryMerged.wkt)
myObj.save(using='u1')

I get the error:

IntegrityError: null value in column "id" violates not-null constraint

Why I get this? Reading the documentation I was expecting that the id value would be chosen automatically using the sequence...

3
  • Did you create the table using syncdb? In my DB all ID fields are serial: id serial NOT NULL, Commented Apr 4, 2012 at 10:01
  • No, I have created the db table and the model separately. Commented Apr 4, 2012 at 10:09
  • 1
    It's been quite some time, but if someone else comes around, you should add managed = False in your model's meta options when you create the tables yourself. Commented Oct 6, 2016 at 13:10

1 Answer 1

5

Actually I am providing this as an answer. The datatype serial in postgres is an auto incremental four-byte integer. If you change your id from integer to serial this would work.

 id serial NOT NULL

Read more: http://www.postgresql.org/docs/8.4/static/datatype.html

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, now it works! The solution was indeed to change "integer" with "serial".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.