I have an existing DB on MSSQL. I imported it into Django using
manage.py inspectdb > manage.py
I ran makemigrations and migrate.
This is an example of one of the models:
class Clerk(models.Model): clientid = models.AutoField(db_column='clientId', primary_key=True) clerkname = models.CharField(db_column='clerkName', max_length=30, default="ClerkNameDefault") ... class Meta: managed = False db_table = 'Clerks'
So far, so good!
However, when I try to make an instance, it looks like I am encountering problems with the clientid.
>>> from webapi.models import Clerk >>> clerk = Clerk(clerkname="hi") >>> clerk.clientid
Strange! No clientid, despite it being set as an AutoField. Could it be that it’s because Managed = False? Well, let’s set the id myself and see where this goes:
>>> clerk.clientid=1 >>> clerk.save() Traceback (most recent call last): File "C:\git\django-mssql\venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "C:\git\django-mssql\venv\lib\site-packages\mssql\base.py", line 576, in execute return self.cursor.execute(sql, params) pyodbc.ProgrammingError: ('42000', " [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Table 'Clerks' does not have the identity property. Cannot perform SET operation. (8106) (SQLExecDirectW)")
Any hints? I hope I provided enough context.
For clarity, what I wanted was for Django to automatically populate the clientid portion, and for save() to write the model into my database.
Edit: 8 hours later, I have no idea if I did the right thing or not, but I went into my table > right click: “Design” > clientId > column properties: Identity Specification: (is identity) and clicked yes there.
I wonder if I need to manually do this for the rest of my tables…