How to Solve Error ERROR: column “column_name” of relation “table_name” does not exist LINE 1: on the Restore Process of a Database in PostgreSQL Database

Posted on

Introduction

Importing or restoring data from an SQL file consisting of INSERT statement stop when an error appear. The following is the error appear preventing the operation for importing or restoring data to go on further :

C:\>psql -Udb_user -d db_app < "C:\Users\Personal\Downloads\insert-active-employee.sql" 
Password for user db_user: 
ERROR: column "employment_date" of relation "org_employee" does not exist 
LINE 1: ...birthdate, birthplace, address, postal_code, status, employment_date...   
^

Actually, this article has some several articles which is quite related upon the process for importing or restoring data from an SQL file. Those articles exist in the article with the title of ‘How to Solve Error Message ERROR: value too long for type character varying(10) on the Restore Process of a Database in PostgreSQL Database’ in this link. Another one also exist in this link with the title of ‘How to Solve Error ERROR: syntax error at or near “[” on the Restore Process of a Database in PostgreSQL Database’. Those error message are facing different types of errors.

In this context, the error type is focusing on the non-existence of a column. Actually, the table with the name of ‘org_employee’ is a Django generated table from executing the migration script. So, apparently after finishing on generating the table, there is a column missing or it is not available after the attempt to run an SQL file to restore records or data rows.

Solution

Normally, the solution just executing a certain query to add the non-existent column in the table. But since the table has a strong connection with the model available in a Django model exist in the Django-based application, there is another suitable way to solve the problem. Those way exist in the following steps :

  1. First of all, just access the ‘models.py’ file in the root folder of the application.

  2. Next, edit the ‘model.py’ file to add the necessary attribute or field which is going to represent the non-existent column. In this context, it is the column with the name of ’employment_date’. The following below is the revision of the class of the model representing the ‘org_employee’ table :

  3. # Create your models here.
    class Employee(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
        name = models.CharField(max_length=100, blank=True, null=True)
        birthdate = models.DateField()
        birthplace = models.CharField(max_length=50, blank=True, null=True)
        address = models.CharField(max_length=255, blank=True, null=True)
        employment_date = models.DateField()
        def __str__(self):
            return self.name
  4. Finally, execute the following command to generate a migration script. Furthermore, that script is important for implementing the new added attribute or field into a new column for the table. The command is ‘python manage.py makemigrations’.

  5. Last but not least, execute the command ‘python manage.py migrate’ to implement the migration script into the table.

  6. Last step, just run the command again for restoring the records or row data to its associated table.

Leave a Reply