简体   繁体   中英

Django & Postgres: persisting 'ProgrammingError: column does not exist'

I know this question has been asked on SO before however I have tried everything that has been suggested in the answers and none seem to work.

Problem: I have a model class called ' Accomm ' in my Django project where I have a number of objects stored. On the web pages where I try to retrieve one or more instances of ' Accomm ' then the following error is raised...

ProgrammingError at /
column objects_accomm.description does not exist
LINE 1: ...omm"."id", "objects_accomm"."mapped_location_id", "objects_a...

This problem started to occur when I migrated a new field ' description ' to the ' Accomm ' model.

Tried solutions: I have attempted to do the following things... clear all Django migrations (using the --zero and --empty commands); clear the database (PostgreSQL 10); clear both the migrations and database; changing the name of my database in the Django project settings.py file. None of these seem to work. I have also followed tutorials here , here and here .

Full error:

Django Version: 1.11.7
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.gis',
 'django.contrib.humanize',
 'django.contrib.messages',
 'django.contrib.sessions',
 'django.contrib.staticfiles',
 'rest_framework',
 'accounts',
 'maps',
 'objects',
 'social_django']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'social_django.middleware.SocialAuthExceptionMiddleware']

Traceback:

    File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py" in inner
      41.             response = get_response(request)

    File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
      187.                 response = self.process_exception_by_middleware(e, request)

    File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
      185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "/Users/jayt/project/main/views.py" in home
      21.     return render(request, 'home.html', {'accomm':accomm})

    File "/Library/Python/2.7/site-packages/django/shortcuts.py" in render
      30.     content = loader.render_to_string(template_name, context, request, using=using)

    File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string
      68.     return template.render(context, request)

    File "/Library/Python/2.7/site-packages/django/template/backends/django.py" in render
      66.             return self.template.render(context)

    File "/Library/Python/2.7/site-packages/django/template/base.py" in render
      207.                     return self._render(context)

    File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
      199.         return self.nodelist.render(context)

    File "/Library/Python/2.7/site-packages/django/template/base.py" in render
      990.                 bit = node.render_annotated(context)

    File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
      957.             return self.render(context)

    File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
      177.             return compiled_parent._render(context)

    File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
      199.         return self.nodelist.render(context)

    File "/Library/Python/2.7/site-packages/django/template/base.py" in render
      990.                 bit = node.render_annotated(context)

    File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
      957.             return self.render(context)

    File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
      72.                 result = block.nodelist.render(context)

    File "/Library/Python/2.7/site-packages/django/template/base.py" in render
      990.                 bit = node.render_annotated(context)

    File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
      957.             return self.render(context)

    File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
      321.             if match:

    File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __nonzero__
      258.         return type(self).__bool__(self)

    File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __bool__
      254.         self._fetch_all()

    File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _fetch_all
      1118.             self._result_cache = list(self._iterable_class(self))

    File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __iter__
      53.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)

    File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
      894.             raise original_exception

    Exception Type: ProgrammingError at /
    Exception Value: column objects_accomm.description does not exist
    LINE 1: ...omm"."id", "objects_accomm"."mapped_location_id", "objects_a...
                                                             ^

Edit: Models.py for 'Accomm':

class Accomm(models.Model):
    mapped_location=models.ForeignKey(Location,related_name='l_accomms',null=True,blank=True,on_delete=models.SET_NULL)
    description=models.CharField(max_length=1000,null=True,blank=True)
    creator=models.OneToOneField(accountmodels.UserProfileModel,related_name='u_creator',null=True,on_delete=models.SET_NULL,blank=True)
    slug_key=models.SlugField(unique=True)
    is_active=models.BooleanField(default=False)

Views.py:

def home(request):
    user = request.user
    accomm = Accomm.objects.annotate(img_count=models.Count('a_image')).filter(img_count__gte=1)[:6]
    return render(request, 'home.html', {'accomm':accomm})

Any other ideas besides the solutions proposed here?

Something I like to try when my migrations get wonky is a something like:

python manage.py makemigrations
python manage.py migrate --fake

So this will make it seem like you model has the added column, so from here I then cut and paste the model into a temporary file, save the models and run the migrations regularly which will remove all of the model. Then you paste the model back into models.py and run migrations one more time. This seems to work for me, there are probably better ways to manage this however I am relatively new to django as I am just completing my first big project in it. Really hope this helps, if not let me know and I may have something else for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM