Join my Asyncio (Python) course on Udemy for free 👀

Solving Django warning `Auto-created primary key used when not defining a primary key type`

April 7, 2021  ‐ 2 min read

While playing around with the newly released Django 3.2 I noticed a warning I didn't see before in a website I migrated from Django 3.1 (Warning models.W042).

System check identified some issues:

users.User: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
HINT: Configure the DEFAULT_AUTO_FIELD setting or the UsersConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.

The warning is introduced with a new feature in Django 3.2 which allows to change the default field of primary which are automatically added by Django if you don't explicitly define one.

You can explicitly define a primary key by adding primary_key=True to a model field. If you don't, Django adds a primary key itself called id. Before Django 3.2, the primary key field that Django would add was of type 'django.db.models.AutoField'. Since Django 3.2 the default has been changed to 'django.db.models.BigAutoField'.

The BigAutoField probably represents a 64-bit positive integer in your database. Where the AutoField most likely a 32-bit positive integer. This change in Django implies that you will be able to store a whole lot more rows in the database table of the model.


In order to get rid of the warning you need to set the DEFAULT_AUTO_FIELD in the settings to the field you prefer for primary keys. If you rather keep the Django 3.1 default you should set the auto-field value to 'django.db.models.AutoField'. This can also prevent some unwanted migrations in the future. You can either set the value in your settings:

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

Or set it per app in the file:

from django.apps import AppConfig

class UsersConfig(AppConfig):
    default_auto_field = 'django.db.models.AutoField'
    name = 'users'

You can of course also explicitly set the primary on the model itself.