'error in migration with legacy database in Django

I'm working on creating a filter in django where the options displayed are coming from a new column in the database. It turns out that this column was created directly in the database and so that it could be displayed in my template, I need to capture this field in the models.

After some research, I found in django's own documentation a function called "inspectdb" that is called by manage.py. So far so good, after executing the function, the database fields are added to my project so that they can be directed to their corresponding app.models.

The documentation indicates that I must perform a python manage.py migrate for the sync to complete, this snippet of code is where the problem happens. When performing the migrate, I get the following error: "django.db.utils.ProgrammingError: relation "crontabmanager" already exists"

The "crontabmanager" table actually exists in my database, but it is not changing at this time.

Some actions were taken to try to get around this problem, for example:

  • I tried to ignore the migration and use the new field directly in the system, but it returns stating that the new column does not exist
  • Delete the migration file and create a new "makemigration"
  • Delete the "crontabmanager" table from the database for django to recreate through the ORM
  • Changing models.py properties to ignore changes made

Below is the snippet of my current models.py code:

from django.db import models


class Crontab(models.Model):

    client = models.TextField('Cliente', blank=True, null=True)
    script = models.TextField('Nome do script', primary_key=True)
    schedule_code = models.TextField('Codigo Crontab', blank=True, null=True)
    crontab_command = models.TextField("Comando", blank=True, null=True)
    log = models.TextField("Log", blank=True, null=True)

    class Meta:
        verbose_name = 'Crontab'
        verbose_name_plural = 'Crontab'
        db_table = "crontabmanager"

class Trello(models.Model):

    id_card = models.TextField('ID do Card', primary_key=True)
    card_name = models.TextField('Nome do Card', blank=True, null=True)
    due_date = models.TextField('Data de conclusão', blank=True, null=True)
    list_name = models.TextField('Nome da lista', blank=True, null=True)
    tipo_corte = models.TextField('Tipo do corte', blank=True, null=True)
    cortes = models.TextField('Numero de cortes', blank=True, null=True)
    unidade = models.CharField(max_length=200, blank=True, null=True) #new field added


    class Meta:
        db_table = "trello_pta"

error when running python manage.py migrate

$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, core, sessions, users
Running migrations:
  Applying core.0002_initial...Traceback (most recent call last):
  File "/home/file_names/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation "crontabmanager" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
...

return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "crontabmanager" already exists


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source