Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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] 
like image 708
Hi There Avatar asked Apr 06 '21 15:04

Hi There


People also ask

How can create primary key in Django model?

By default, Django adds an id field to each model, which is used as the primary key for that model. You can create your own primary key field by adding the keyword arg primary_key=True to a field. If you add your own primary key field, the automatic one will not be added.

What is Default_auto_field?

default_auto_field attribute. No more needing to override primary keys in all models. Maintaining the historical behavior, the default value for DEFAULT_AUTO_FIELD is AutoField . Starting with 3.2 new projects are generated with DEFAULT_AUTO_FIELD set to BigAutoField . Also, new apps are generated with AppConfig.

Does Django automatically create ID?

Django will create or use an autoincrement column named id by default, which is the same as your legacy column.

What is the default primary key in Django?

Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added). Changed in Django 3.2: In older versions, auto-created primary key fields were always AutoField s.


1 Answers

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)     ... 
like image 151
Nuts Avatar answered Sep 28 '22 04:09

Nuts