Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'

Why this is showing a warning as it default take the primary key from the User model so should I also declare the primary key the Registration or candidate model again. models.py

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


class Registration(models.Model):
    fname = models.CharField(max_length=30)
    lname = models.CharField(max_length=30)
    # phone = models.BigIntegerField(max_length=10,primary_key=True)
    dob=models.DateField()
    user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
    def __str__(self):
        return self.fname

class candidate(models.Model):
    full_name = models.CharField(max_length=30)
    position = models.CharField(max_length=30)
    total_vote = models.IntegerField(default=0)
    def __str__(self):
        return "{} -- {}".format(self.full_name,self.position)

problem occured

WARNINGS:
poll.candidate: (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 PollConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
like image 742
Anil Chaudhary Avatar asked Jun 01 '21 05:06

Anil Chaudhary


People also ask

What is the default primary key in Django?

If you don't specify primary_key=True for any fields in your model, Django will automatically add an IntegerField to hold the primary key, so you don't need to set primary_key=True on any of your fields unless you want to override the default primary-key behavior. For more, see Automatic primary key fields.

What is AutoField in Django?

AutoField. An IntegerField that automatically increments according to available IDs. You usually won't need to use this directly; a primary key field will automatically be added to your model if you don't specify otherwise.

Can Django model have two primary keys?

Do Django models support multiple-column primary keys? ¶ No. Only single-column primary keys are supported.

What is the default primary key type in Django?

Starting new projects in Django 3.2, the default type for primary keys is set to a BigAutoField which is a 64 bit integer. However, earlier versions set the type of implicit primary keys to be integers. This means that when you upgrade to 3.2, you will start to see warnings about the fact that you do not have an explicitly defined primary key type.

What is w042 key in Django?

(models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.#348 Open dmsimardopened this issue Jan 18, 2022· 0 comments Open (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.#348

How to set default auto field to default in Django?

You should set DEFAULT_AUTO_FIELD explicitly to current DEFAULT_AUTO_FIELD value 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'

How to get rid of the warning that primary keys are missing?

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


1 Answers

When you define a model in Django without specifying a primary key, Django will automatically create a primary key for you. The primary key is set to be an integer. If you want to override the field type, you can do so on a per model basis.

Starting in Django 3.2, you can now customise the type of the automatically created primary key in your settings.

Starting new projects in Django 3.2, the default type for primary keys is set to a BigAutoField which is a 64 bit integer. However, earlier versions set the type of implicit primary keys to be integers.

This means that when you upgrade to 3.2, you will start to see warnings about the fact that you do not have an explicitly defined primary key type. Satisfying Django's requirements for an explicitly set primary key type is easy enough, but you also need to choose whether or not you want to upgrade your primary key field types from integer to 64 bit integer.

There are a few ways to fix this. Broadly speaking they fall into two categories

Configure DEFAULT_AUTO_FIELD in settings

Open your project's settings.py and add a new line at the bottom of the file

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

Setting on a per app basis

If you prefer to set your field type on a per app basis rather than for the whole project, you can specify this in apps.py.

from django.apps import AppConfig

class MyappConfig(AppConfig):
    default_auto_field = 'django.db.models.AutoField'
    name = 'Myapp'

Set AutoField or BigAutoField on a per model basis

If you prefer even more fine grained control, you can set a per model id field.

class Model1(models.Model):
    id = models.BigAutoField(primary_key=True)
like image 190
Ashish Nautiyal Avatar answered Oct 02 '22 15:10

Ashish Nautiyal