Auto-create primary key used when not defining a primary key type warning in Django Auto-create primary key used when not defining a primary key type warning in Django python-3.x python-3.x

Auto-create primary key used when not defining a primary key type warning in Django


Your models do not have primary keys. But they are being created automatically by django.

You need to choose type of auto-created primary keyshttps://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys (new in Django 3.2)

Either add this into settings.pyDEFAULT_AUTO_FIELD='django.db.models.AutoField'

or

class Topic(models.Model):    id = models.AutoField(primary_key=True)    ...


You have unintentionaly updated Django to 3.2 which warns you and as hint text suggest you have to set DEFAULT_AUTO_FIELD as documentedThis way you avoid unwanted migrations in future releases asvalue of DEFAULT_AUTO_FIELD will be changed to BigAutoField

You should set DEFAULT_AUTO_FIELD explicitly to current DEFAULT_AUTO_FIELD value

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

you could configure it even per app basis ( if you expect to build new apps with current style primary key)

from django.apps import AppConfigclass MyAppConfig(AppConfig):    default_auto_field = 'django.db.models.AutoField'    name = 'my_app'

or even per-model ( discuraged )

from django.db import modelsclass MyModel(models.Model):    id = models.AutoField(primary_key=True)

You should also take care of version locking your requirements as you could introduce backward incompatible changes in production


new created project settings.py file in django 3.2includes this:

..# Default primary key field type# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-fieldDEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

and as your project was created on earlier versions of django so you can append this to your settings.