Stop auto-increment primary key

Is it possible to stop Django from trying to creating a value for the primary key?

I can do this a sproc, if I have to, but would like to see if a Model will do this. My DB is MS SQL server and it’s a so called “legacy” table i.e. database first :

class ClinicalNotes(models.Model):

ClinicalNoteId = models.IntegerField(primary_key=True)
RefId = models.ForeignKey(MasterActivityTable, on_delete=models.DO_NOTHING, db_column="RefId")
ClinicalNote = models.TextField()
NoteType = models.CharField(max_length=20)
MPI = models.IntegerField()
CN_DCTM_sts = models.IntegerField()
Hidden = models.IntegerField()
RelatedObjectId = models.CharField(max_length=50)
ActionStatus = models.CharField(max_length=20)
CreatedDate = models.DateTimeField()
CreatedTime = models.DateTimeField()
CreatedBy = models.CharField(max_length=20)

class Meta:

A model has to have a primary key, but when I create a new record and save it, I get a SQL ODBC error saying the primary key, which has an IDENTITY clause that means SQL creates the Id, cannot be set by the calling procedure. Obviously Django is behaving as if it’s in charge when on this occasion, it is not.

Is it possible to continue to use the model or will I have to resort to a sproc?

Ta very, Paul


Hi Paul,

Assuming you’re using mssql-django, you may want to try using an AutoField. It looks like that backend will assume those fields use an Identity column as per this part of the code.