[Solved] Auto-created primary key used when not defining a primary key type in Django

[Solved] Auto-created primary key used when not defining a primary key type in Django

Are you getting the following error while running your Django application?

main.ContactModel: (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 MainConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.

You will come across this error majorly when you upgrade your Django app library to version 3.2.

In my case, I migrated my Django app module from version 3.1.3 to version 3.2.20 and I face this issue.

In Django, while creating a model, if you don’t specify the primary key, the primary key will be created automatically.

In the Django 3.2, the default auto field datatype is BigAutoField. And in the previous version, the auto field datatype was AutoField.

While developing my Django app, I created Django models. The auto primary key had the datatype AutoField. Migrating this auto field datatype is cumbersome. It required Django app model/database migration.

The best solution is to, set the default auto field value to AutoField.

The simple solution is just two steps.

  • Open your settings.py Python file.
  • Add below the line of code at the end of the file.
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

Now, you run your application. Hurry! You will not see any errors. Your application is running successfully.

No more database migration 😀

If you are still getting challenged by this issue, write me in the comment section below. I will help you at my best.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *