Do id primary key have to be explicitly defined in Django models generated from inspectdb against an existing database?
Why
'id'
primary key is missing from Django models definitions and should I add this field to the models classes?
No. If you do not specify a primary key in your models. Django will automatically add a AutoField
to your model named id
, as is specified in the documentation on Automatic primary key fields:
By default, Django gives each model the following field:
id = models.AutoField(primary_key=True)
This is an auto-incrementing primary key.
If you'd like to specify a custom primary key, specify
primary_key=True
on one of your fields. If Django sees you've explicitly setField.primary_key
, it won't add the automaticid
column.