Admin site not inserting Id value

I’ve set up a very generic admin function and when saving am getting the following error:

This is for this admin definition:

from django.contrib import admin
from GeneticClinic.models import HPOTerm
from GeneticClinic.models import HPOTermSynonym
from GeneticClinic.models import HPOTermNegative
from GeneticClinic.models import HPOTermSynonymNegative

class HPOTermAdmin(admin.ModelAdmin):
    list_filter = ('TermCode', 'Term',)
    readonly_fields = ('CreatedDate', 'CreatedBy', )
    pass

class HPOTermSynonymAdmin(admin.ModelAdmin):
    pass

class HPOTermNegativeAdmin(admin.ModelAdmin):
    pass

class HPOTermSynonymNegativeAdmin(admin.ModelAdmin):
    pass

admin.site.register(HPOTerm, HPOTermAdmin)
admin.site.register(HPOTermSynonym, HPOTermSynonymAdmin)
admin.site.register(HPOTermNegative, HPOTermNegativeAdmin)
admin.site.register(HPOTermSynonymNegative, HPOTermSynonymNegativeAdmin)

And by way of example, the HPOTermNegative model (for which the error is happening):

class HPOTermNegative(models.Model):
    Id = models.AutoField(primary_key=True)
    HPOTerm = models.ForeignKey(HPOTerm, on_delete=models.DO_NOTHING, db_column="HPOTermId")
    TermNegative = models.CharField(max_length=2000)
    CreatedDate = models.DateTimeField()
    CreatedBy = models.CharField(max_length=250)
    UpdateDate = models.DateTimeField(blank=True)
    UpdateBy = models.CharField(max_length=250, blank=True)

    class Meta:
        managed=False
        db_table='HPOTermNegative'

    def __str__(self):
        return self.HPOTerm.TermCode + " : " + self.HPOTerm.Term + " -ve: " + self.TermNegative

The database is SQL server. I’m not sure if that makes a difference…it usually does.

I don’t have any issues elsewhere when I write code to add a similar database entity…for example:

            _clinicNotesHPOTerm = ClinicalNotesHPOTerm()

            _clinicNotesHPOTerm.ClinicalNote = _clinicalNote
            _clinicNotesHPOTerm.HPOTerm = _hpoTermItem
            _clinicNotesHPOTerm.CreatedDate = datetime.today()
            _clinicNotesHPOTerm.CreatedBy = request.user.username

            _clinicNotesHPOTerm.save()

Works fine. And this has exactly the same sort of model/DB definition.

I can just build by own maintenance screens for these four tables and that may be a better way of doing this anyway…I’m just curious as to whether anyone recognises the issue here?

You’ve got HPOTermNegative defined as managed = False, which means Django is expecting the table to already exist in the form that it needs to be. Check the underlying table to verify that the Id column is the primary key and is an auto field. (I don’t know how they’re implemented in SQL Server, so I don’t know precisely what to look for.)