Django with MS SQL Server

Hello, I am working with an MS SQL server. There is a primary already set in a table called ‘UniqID’
I made my custom primary key ‘Autofield’
when I try to add data it throws an error that UniID is not null. It is not auto Incremented the UniqID.

uniqid = models.AutoField(primary_key=True, db_column=‘UniqID’, null=False)

Error: (‘23000’, "[23000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot insert the value NULL into column UniqID

Anyone can help.
Thank you

If UniqID is not an auto-incremented column, you should use IntegereField (or BigIntegerField) instead of AutoField.

Then it will be not incremented field. Every time I have to put the manually primary key.

OK, I’m confused. What is the data type of UniqID in your database? It looks like an ordinary numeric field with no sequence attached. What 3rd-party database backend are you using?

Its data type is numeric. I am using an MS SQL server. I have a database already. I fetched the models via inspect db.
I got the entity like this.
uniqid = models.DecimalField(db_column=‘UniqID’, primary_key=True, max_digits=18, decimal_places=0) # Field name made lowercase.

It is a primary key in database. I changed to:
uniqid = models.AutoField(primary_key=True, db_column=‘UniqID’, null=False)

The error I am getting while in POST request.

IntegrityError at /signup/

(‘23000’, “[23000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot insert the value NULL into column ‘UniqID’, table ‘DCD_Prod_6Feb2020.dbo.DryClean_mCustomerInfoMaster’; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW)”)

Your field is a NUMERIC column not an auto-incremented column, it should be IDENTITY in MSSQL. Django is not at fault. You can provide uniqid manually or alter data type to the IDENTITY(starting value, 1) which can be tricky. You need to remember to use an appropriate starting value, i.e. the maximum from the existing values + 1.

Yes, this is the problem. Thank you

1 Like