Introduction
Another article containing subject about how to solve an error message appear while running a Django-based web application. The error message appear when running a command for starting an internal service for accessing the Django-based web application. The following is the actual image of the error appearance :
Before continuing on to the discussion of solving the error message, it is important to find out the current condition of the project itself. Those condition exist as follows :
-
There is a project using Django-based framework where the base of it exist in this link. It is an article with the title of ‘How to Present or Display a File using the HttpResponse Object in Django Application’.
-
Without having an application, the project is using the url, model and view of the project itself to be defined. In this context, the project name is ‘myproject’.
-
Before going further, the first one is the definition of an URL address for displaying a page in the urls.py file. It exist in the root folder of the project with the content as follows :
path('personnel', views.list_personnel),
-
Next, check the function of ‘list_personnel’ in the views.py file as defined in the urls.py for accessing the URL of ‘personnel’. This is the ‘list_personnel’ function in the views.py file :
def list_personnel(request): context = {} context['data'] = Personnel.objects.all() print(context['data']) form = PersonnelForm(request.POST or None) if form.is_valid(): form.save() context['form'] = form return render(request, "list_personnel.html", context)
The error appear upon executing ‘Personnel.objects.all()’ where it will try to retrieve data from the table of personnel exist in database myproject. For the table name’s pattern, it will be project-or-app-name_model-name. For an example, the project name is ‘myproject’ and the model is ‘Personnel’. So, the table name will be ‘myproject_personnel’.
-
The database connection configuration exist in a file with the name of ‘settings.py’. Normally, it exist in folder of the project. The following content is displaying the configuration lines describing the database connection configuration :
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'USER': 'django_postgres', 'NAME': 'sysapp', 'PASSWORD': 'password', } }
Although the database connection configuration is correct, if the table does not exist, it will trigger the error message. The reason is executing to fetch all from the model class of ‘Personnel’ means selecting all data from table personnel. In this example, considering the name pattern, the table name is ‘myproject_personnel’. Check the database.
Solution
The solution for the problem which is triggering the error message is just simple. In the above image at the introduction part, the error is because of the non-existence of the table. In other words, relation does not exist means that the table is not exist which in this context it is ‘myproject_personnel’. For the full complete URL pattern of the project, the following is the content of the ‘urls.py’ file :
from django.contrib import admin from django.urls import path, include, reverse from django.views.generic import RedirectView from . import views urlpatterns = [ path('admin/', admin.site.urls), path('personnel', views.list_personnel), ]
Since the cause is because of the non-existent of the table, the following are the step for solving the error message :
-
First of all, just access the database. Check the existence of the table. The following is the attempt to check it :
(env) C:\programming\python\django\myproject>psql -Udjango_postgres sysapp Password for user django_postgres: psql (14.0) WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. sysapp=> \dt sysapp=> \dt myproject_personnel Did not find any relation named "myproject_personnel". sysapp=>
-
Since the table is not exist, just execute the following command to create migration scripts :
(env) C:\programming\python\myproject>python manage.py makemigrations myproject Migrations for 'myproject': myproject\migrations\0001_initial.py - Create model Personnel (env) C:\programming\python\myproject>
-
After successfully creating a migration which is according to the above output command is referring to create the migration script for model with the name of ‘Personnel’, continue on to the next step. It is a command to execute the migration script by typing the following for further execution :
(env) C:\programming\python\myproject>python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, flatpages, myproject, sessions, sites Running migrations: Applying myproject.0001_initial... OK (env) C:\programming\python\myproject>
-
Finally, access the database again and check for the existence of the table as follows :
sysapp=> \d+ myproject_personnel; Table "public.myproject_personnel" Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description --------+------------------------+---------------+----------+-------------------------------------------------+----------+-------------+--------------+------------- id | bigint | | not null | nextval('myproject_personnel_id_seq'::regclass) | plain | | | name | character varying(100) | | not null | | extended | | | Indexes: "myproject_personnel_pkey" PRIMARY KEY, btree (id) Access method: heap sysapp=>
-
Last but not least, try to execute the Django-based internal service for running the Django-based application once more. If there are no more error messages appear, the application will run properly.