I have a Django ORM model that needs to integrate with a legacy database. The model was generated via manage.py inspectdb, and the class definition is like so:
class ClientJob(models.Model):
id = models.AutoField(primary_key=True, db_column="id")
CustomerGuid = models.CharField(max_length=40, db_column='CustomerGUID', blank=True)
JobGuid = models.CharField(max_length=40, db_column='JobGUID', blank=True)
AgentGuid = models.CharField(max_length=40, db_column='AgentGUID', blank=True)
class Meta:
db_table = u'ClientJob'
The primary key id was originally defined as models.IntegerField(primary_key=True), but from my understanding of Django this needs to be an AutoField if I want it to automatically increment hence the change.
I can query for objects without any issues, but when I run into trouble when I attempt to create and save a new object. The following code throws an IntegrityError with the message “null value in column “id” violates not-null constraint”.
new_job = ClientJob.objects.create(CustomerGuid=customer_guid, JobGuid=str(uuid4()), AgentGuid=agent_guid)
new_job.save()
I suspect (but by no means certain) that this might be because my ClientJob table’s primary key depends on a custom sequence. The definition of the sequence is as follows:
CREATE SEQUENCE seq_client_job_id
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 11020
CACHE 1;
ALTER TABLE seq_client_job_id
OWNER TO ssa;
Any help shedding light on this will be much appreciated.
Simply delete or uncomment the
idline in your modelclass – it should work like a charm.