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

Python 3.xDjango

Python 3.x Problem Overview


I just updated my python from 3.9.1 to 3.9.4. When I tried to run the server. The console gave me a warning for this:

WARNINGS:
learning_logs.Entry: (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 LearningLogsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
learning_logs.Topic: (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 LearningLogsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
No changes detected in app 'learning_logs'

May I please know how do I fix this. I read the documentation about this, but I don't understand how this part this page relates to this.

Models.py

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Topic(models.Model):
    text = models.CharField(max_length = 200)
    date_added = models.DateTimeField(auto_now_add = True)
    image = models.ImageField(upload_to = 'backgroud_images', null = True, blank = True)
    owner = models.ForeignKey(User,on_delete = models.CASCADE)
    def __str__(self):
        return self.text



class Entry(models.Model):
    topic = models.ForeignKey(Topic,on_delete = models.CASCADE)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add = True)

    class Meta:
        verbose_name_plural = "Entries"

    def __str__(self):
        return self.text[:50]

Python 3.x Solutions


Solution 1 - Python 3.x

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

You need to choose type of auto-created primary keys https://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.py DEFAULT_AUTO_FIELD='django.db.models.AutoField'

or

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

Solution 2 - Python 3.x

You have unintentionaly updated Django to 3.2 which warns you and as hint text suggest you have to set DEFAULT_AUTO_FIELD as documented This way you avoid unwanted migrations in future releases as value 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 AppConfig >
> class MyAppConfig(AppConfig): > default_auto_field = 'django.db.models.AutoField' > name = 'my_app'

or even per-model (discouraged)

> from django.db import models >
> class 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

Solution 3 - Python 3.x

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

..

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_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.

Solution 4 - Python 3.x

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

primary_key=True https://docs.djangoproject.com/en/3.2/ref/models/fields/#primary-key

settings.py:
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

DEFAULT_AUTO_FIELD https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

Solution 5 - Python 3.x

A simple solution:

Django wants you to add the serial number that's called by primary key and if you can ignore this warning then okay django will automatically add the primary key otherwise, if you want to remove this warning then add this code in your defined model:

id = models.AutoField(primary_key=True)

For example:

class Contact(models.Model):
    sno = models.AutoField(primary_key=True)
    name = models.CharField(max_length=25, blank=True)
    email = models.EmailField(max_length=40, blank=True)
    phone = models.IntegerField()

HaPpY Coding 珞

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHi ThereView Question on Stackoverflow
Solution 1 - Python 3.xNutsView Answer on Stackoverflow
Solution 2 - Python 3.xiklinacView Answer on Stackoverflow
Solution 3 - Python 3.xAli ArefView Answer on Stackoverflow
Solution 4 - Python 3.xHimaloy MondalView Answer on Stackoverflow
Solution 5 - Python 3.xMayur GuptaView Answer on Stackoverflow