Django Programming Error Column does not exist.

In this Python Django Tutorial, we are going to discuss the most common Django error Django Programming Error Column does not exist that we face generally when we register models in our Python Django project.

Recently when working on a Django Project, I faced this error so, we will learn how to fix the Django Programming Error and we will discuss the reasons for its occurrence so that we can avoid this error in our further Python Django Projects.

Django Programming error
Django Programming error

Solve Django “Programming Error : column does not exist”

In the following points, we will discuss the number of reasons which are responsible for this error.

1. Running Migrations

Running Migrations after creating models should be a must or when you make any changes to models or tables you should run migrations. To run the migrations in your Python Django project follow the below two commands in your terminal or command prompt by locating the project directory.

python manage.py makemigrations
pytohn manage.py migrate

After running migrations, all the changes will be applied in the project, and register all the changes we have made. To avoid this error you can check the migrations which are not applied before running the server. We can check those unapplied migrations by running the below command in the terminal.

python manage.py showmigrations

2. Check database schema

If you are going to insert a column then make sure that column exists in the Database schema. Sometimes it happens that you might have entered the wrong or incorrectly referenced column name so, check the column name in the code and check the column name is the database.

READ:  Python Django "Module not found" error.

Make sure to run migrations to update the database schema by running the command “python manage.py migrate”

3. Other Scenarios

In My case when I was working on a Python Django project where I created model tables and made migrations but later there was a need to change the application name where the name of the application was earlier UserRegistation and I had to change it to JobSeeker.

That time it was throwing the error as shown in the above picture because there were changes applied in the models from the previously applied migrations. Since the models are there in the app directory so migrations store the app name also.

To solve this error in this condition you need to go to the migrations folder where it has stored all the changes we have made in each migration.

Django Error Programming error
Django Error Programming error

Here you can see the list of migrations (0001, 0002, 003) which are stored in the migrations folder. Next, go to the migrations you have made before applying changes.

Python Django Programming Error

In the above picture, you can see the migrations are still stored with the old application name which was UserRegistraion and later it was changed to JobSeeker. To fix this thing we need to change the code as shown in the below code.

migrations.AlterField(
            model_name='usercertification',
            name='user_id',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='UserRegistration.userregistration'),

We will changes to the code as shown in the below code.

migrations.AlterField(
            model_name='usercertification',
            name='user_id',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='JobSeeker.userregistration'),

We need to make in every particular line of code where the previously installed app is being registered and change it to the new name of the app. After that run migrations to apply these changes in your project.

READ:  Build a Django Contact Form with Email

Conclusion:

In this Python Django Tutorial, we have learned about all possible reasons why we face this Django Programming Error Column does not exist. Later we discussed all the methods to solve this error.

We learned that the most common mistake we do is not running migrations after creating models or after making changes in the models.

You may also like to read: