# Stop auto-increment primary key

**URL:** <https://forum.djangoproject.com/t/stop-auto-increment-primary-key/9876>\
**Category:** Using Django\
**Created:** [September 28, 2021, 2:39pm UTC](https://forum.djangoproject.com/t/stop-auto-increment-primary-key/9876 "2021-09-28T14:39:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmasonBWC](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pmasonbwc/32/5135_2.png) [@pmasonBWC](https://forum.djangoproject.com/u/pmasonBWC)\
**Post date:** [September 28, 2021, 2:39pm UTC](https://forum.djangoproject.com/t/stop-auto-increment-primary-key/9876/1 "2021-09-28T14:39:29Z")

</div>

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:
    managed=False
    db_table='ClinicalNotes'

```

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

hgghfghfg

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [September 28, 2021, 3:30pm UTC](https://forum.djangoproject.com/t/stop-auto-increment-primary-key/9876/2 "2021-09-28T15:30:15Z")

</div>

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](https://github.com/microsoft/mssql-django/blob/ce3edcac06bb18f00896dc3fa770c0f9cb8606ac/mssql/base.py#L104-L108).
