How to Solve Error Message django.db.utils.ProgrammingError: relation “table_name” does not exist because of migration execution in Django Application

Posted on

Introduction

Basically, this article already exist previously but with a different context. That article exist in this link where the title is ‘How to Solve Error Message Programming Error relation does not exist on accessing Django Application’. In that article, the situation triggering the error message is because of the non-existence of a table. It is a table which is part of the project definition. In detail, it is the models.py exist in the folder of the project.

So, executing ‘python manage.py makemigrations’ will automatically execute all of the models available in the ‘models.py’ file. The result will be a generated migration script files. Next, following after with the execution of ‘python manage.py migrate’, it will run the migration script. It will implement all migration script into the database. Obviously, it is all the migration script available which is not being implemented to the database. which exist from the previous command execution.

But apparently, after executing those two commands, in some cases the error persist. The error still appear and it is not solving the problem.

Solution

The following is an actual solution to solve the problem. As going through the source of the applications, there is a several condition which is affecting the process for solving the problem. Those are as follows :

  1. First of all, the table which is not exist by the information in the error message relation does not exist is not in the project. The following is the structure of the Django project.

    (env) C:\python\programming\myproject>tree .
    Folder PATH listing for volume Windows
    Volume serial number is 00000020 E003:3593
    C:\PYTHON\PROGRAMMING\MYPROJECT
    ├───article
    │ ├───migrations
    │ └───__pycache__
    ├───myproject
    │ ├───static
    │ │ └───assets
    │ │ ├───css
    │ │ ├───fonts
    │ │ │ ├───flaticon
    │ │ │ ├───fontawesome
    │ │ │ ├───simple-line-icons
    │ │ │ └───summernote
    │ │ ├───img
    ...
    │ │ ├───js
    ...
    │ ├───staticfiles
    │ ├───templates
    │ └───__pycache__
    └───staticfiles
    (env) C:\python\programming\myproject>
    

    So, executing the command ‘python manage.py makemigrations’ and ‘python manage.py migrate’ in this case it is performing just on the scope of the project. The project above name is ‘myproject’. There is a models.py file inside of the ‘myproject’s folder. The command only automatically generating migration script and implementing the script to the database only to the models exist in the models.py available in the ‘myproject’s folder. But not for the models which is defined in the ‘models.py’ file inside the apps folder.

  2. In order to generate migration script files and implementing it into the database, just modify the previous command. But add another parameter in the command for defining the model’s target of the application for generating the migration script and implement it into the associated table. Without a parameter, by default it will process the model in the models.py file exist in the folder project. By giving a parameter which is an application name for an example ‘apps’, it will be a different result. It will generate migration script file as defined in the models.py exist in the apps folder. Furthermore, by passing the application name, it will also implement it into the database. The following are the executions of the command with additional parameter of application name :

    (env) C:\python\programming\myproject>python manage.py makemigrations article
    Migrations for 'article':
    artikel\migrations\0001_initial.py
    - Create model Category
    - Create model Tag
    - Create model Content
    (env) C:\python\programming\myproject>python manage.py migrate article
    Operations to perform:
    Apply all migrations: artikel
    Running migrations:
    Applying artikel.0001_initial... OK
    
    (env) C:\python\programming\myproject>

    The above example is generating migration script and implementing it into the database. Specifically, it is intended for the models defined in the models.py of the application with the name of ‘article’.

2 thoughts on “How to Solve Error Message django.db.utils.ProgrammingError: relation “table_name” does not exist because of migration execution in Django Application

  1. your English is so horrible that there is no way to understand the mess that you are trying to show, it is a disaster and you are explaining nothing.

    1. I am terribly sorry for my horrible English… Moreover, I am also sorry it become a disaster and it also explain nothing to you… Thanks for commenting. I hope I can write at least a slight better one in the future…

Leave a Reply