1

Django 2.0, python 3.5, postgres

I am trying to create a new boolean field on a model in django. The problem is that when i run makemigrations it throws a

psycopg2.ProgrammingError: column core_marca.eh_maiores_bancos does not exist.

But the eh_maiores_bancos column is the one i am trying to create. This error happens before i can create the migration files. I have had this same problem before with other models and solved by commenting a piece of the code that was causing the exception, but as it keeps happening i am looking for a better solution than looking at the log and manually commenting the lines that cause the error.

Note: The piece of code causing the error is in a totally different file, the views.py file in my API module, in which i have a function that returns a list of all the objects of that model i have.

I Tried dropping the database, recreating it, deleting migration files and then creating migrations and migrating but still fails. I also tried changing the imports sequence on the code, the order of the installed apps, adding a if not 'makemigrations' in sys.argv: to prevent the code from running when i am creating migrations but it just creates a different error.

I found nothing about this in the django documentation. The other stackoverflow question i found with a similar problem (Django Migration Error: Column does not exist) has answers but all of them just tell me to comment the code causing the problem and makemigrations.

The log when running makemigrations is:

Traceback (most recent call last):
##Removed a few lines of traceback. just django core code


  File "/home/esb/projects/ts2/esb-python/gerentesonhos/apps/API/base/urls.py", line 2, in <module>
    from .views import *
  File "/home/esb/projects/ts2/esb-python/gerentesonhos/apps/API/base/views.py", line 126, in <module>
    class MarcaList(generics.ListCreateAPIView):
  File "/home/esb/projects/ts2/esb-python/gerentesonhos/apps/API/base/views.py", line 175, in MarcaList
    for m in marcas:
  File "/home/esb/projects/ts2/esb-env/lib/python3.5/site-packages/django/db/models/query.py", line 272, in __iter__
    self._fetch_all()
  File "/home/esb/projects/ts2/esb-env/lib/python3.5/site-packages/django/db/models/query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/esb/projects/ts2/esb-env/lib/python3.5/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/esb/projects/ts2/esb-env/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1067, in execute_sql
    cursor.execute(sql, params)
  File "/home/esb/projects/ts2/esb-env/lib/python3.5/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/esb/projects/ts2/esb-env/lib/python3.5/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/esb/projects/ts2/esb-env/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/esb/projects/ts2/esb-env/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/esb/projects/ts2/esb-env/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column core_marca.eh_maiores_bancos does not exist
LINE 1: ..._emprestimo", "core_marca"."tipo_instituicao_id", "core_marc...
  • 1
    This is a rather frequently asked question. Something in your code is triggering a query at import time, so it runs before the migration itself. In your case it is the `for m in marcas` in MarcaList, which appears to be in the class definition itself rather than in a method. – Daniel Roseman Jan 30 '19 at 14:30
  • Thank you a lot, that was the problem, i changed the code so that it is no longer a class but a method, i didn't really need a class. Thank you a lot – Lucca Augusto Jan 30 '19 at 16:12

0 Answers0