Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: blog.Comment: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'

Tags:

python

django

mysite-virtualenv) 10:43 ~/django-blog (master)$ ./manage.py migrate
System check identified some issues:
WARNINGS:
blog.Comment: (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 BlogConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'dja
ngo.db.models.BigAutoField'.
blog.Post: (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 BlogConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'dja
ngo.db.models.BigAutoField'.
users.Profile: (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. 'dj
ango.db.models.BigAutoField'.

Operations to perform: Apply all migrations: admin, auth, blog, contenttypes, sessions, users Running migrations: No migrations to apply.

like image 414
Martin Avatar asked Oct 19 '25 10:10

Martin


1 Answers

From the Auto Created Primary Key [Django Doc]

To avoid unwanted migrations in the future, either explicitly set DEFAULT_AUTO_FIELD to AutoField

Add DEFAULT_AUTO_FIELD = 'django.db.models.AutoField' in your settings.py

If you want to set your field type per app basis then you can specify per app basis

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    default_auto_field = 'django.db.models.AutoField'
    name = 'my_app'

or even you can specify per model basis as

from django.db import models

class MyModel(models.Model):
    id = models.AutoField(primary_key=True)
like image 62
user8193706 Avatar answered Oct 22 '25 01:10

user8193706



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!