django.db.utils.ProgrammingError: column calculator_calculation._id does not exist using heroku and djongo

Dylan Airhart

My app works on my local machine but when deployed to heroku with the exact same code I get errors when using get requests to my API views involving any DB interaction. I have a feeling heroku is trying to use postgres for some reason, even though I do not have any code dealing with postgres in my code. Any help is appreciated, I have done migrations a million different ways with no luck, even though mongo shouldnt need migrations.

Error and code below

2019-11-21T18:26:15.428986+00:00 app[web.1]: Traceback (most recent call last):
2019-11-21T18:26:15.428989+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
2019-11-21T18:26:15.428991+00:00 app[web.1]:     response = get_response(request)
2019-11-21T18:26:15.428993+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
2019-11-21T18:26:15.428995+00:00 app[web.1]:     response = self.process_exception_by_middleware(e, request)
2019-11-21T18:26:15.428997+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
2019-11-21T18:26:15.429000+00:00 app[web.1]:     response = wrapped_callback(request, *callback_args, **callback_kwargs)
2019-11-21T18:26:15.429002+00:00 app[web.1]:   File "/app/calculator/views.py", line 22, in calculate
2019-11-21T18:26:15.429004+00:00 app[web.1]:     print(calcs)
2019-11-21T18:26:15.429006+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __repr__
2019-11-21T18:26:15.429008+00:00 app[web.1]:     data = list(self[:REPR_OUTPUT_SIZE + 1])
2019-11-21T18:26:15.429010+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 274, in __iter__
2019-11-21T18:26:15.429012+00:00 app[web.1]:     self._fetch_all()
2019-11-21T18:26:15.429014+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
2019-11-21T18:26:15.429016+00:00 app[web.1]:     self._result_cache = list(self._iterable_class(self))
2019-11-21T18:26:15.429018+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__
2019-11-21T18:26:15.429020+00:00 app[web.1]:     results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
2019-11-21T18:26:15.429022+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
2019-11-21T18:26:15.429024+00:00 app[web.1]:     cursor.execute(sql, params)
2019-11-21T18:26:15.429026+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
2019-11-21T18:26:15.429028+00:00 app[web.1]:     return super().execute(sql, params)
2019-11-21T18:26:15.429030+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
2019-11-21T18:26:15.429032+00:00 app[web.1]:     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
2019-11-21T18:26:15.429034+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
2019-11-21T18:26:15.429036+00:00 app[web.1]:     return executor(sql, params, many, context)
2019-11-21T18:26:15.429038+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
2019-11-21T18:26:15.429040+00:00 app[web.1]:     return self.cursor.execute(sql, params)
2019-11-21T18:26:15.429042+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
2019-11-21T18:26:15.429044+00:00 app[web.1]:     raise dj_exc_value.with_traceback(traceback) from exc_value
2019-11-21T18:26:15.429046+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
2019-11-21T18:26:15.429048+00:00 app[web.1]:     return self.cursor.execute(sql, params)
2019-11-21T18:26:15.429050+00:00 app[web.1]: django.db.utils.ProgrammingError: column calculator_calculation._id does not exist
2019-11-21T18:26:15.429052+00:00 app[web.1]: LINE 1: SELECT "calculator_calculation"."_id", "calculator_calculati...
2019-11-21T18:26:15.429054+00:00 app[web.1]:                ^
2019-11-21T18:26:15.429062+00:00 app[web.1]:

I have everything imported correctly and settings are correct as far as I know for my mongoDB

import os
import environ
import django_heroku



env = os.environ

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))



SECRET_KEY = 'secret'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

STATICFILES_DIRS = (
)
# STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'calculator.apps.CalculatorConfig'
]

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',
]

ROOT_URLCONF = 'sezzleCalculator.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'sezzleCalculator.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "djongo",
        "NAME": "appDB",
        "HOST": "hostURL",
        "PASSWORD": "password",
        "USER": "randomuser",
        "AUTH_MECHANISM": 'SCRAM-SHA-1',
        "AUTH_SOURCE": "admin",
        "ENFORCE_SCHEMA": False,
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'
django_heroku.settings(locals())

It errors on this get request.

def calculate(request):
    if request.method == "GET":
        calcs = Calculation.objects.filter().order_by("_id")
        print(calcs)
        calcsReversed = calcs.reverse()
        response = {"status": "1", "message": ("OK")}
        idx = 0
        for calc in calcsReversed:
            if idx > 9:
                calc.delete()
            response[idx] = {"first":calc.first,"second":calc.second,"operator":calc.operator,"result":calc.result}
            idx +=1
        print(response)
        return HttpResponse(json.dumps(response), content_type="application/json")
    elif request.method == "POST":
        jsonIncoming = json.loads(request.body.decode("utf8"))
        print(jsonIncoming)
        calculation = Calculation(result=jsonIncoming["result"], first=jsonIncoming["first"],second=jsonIncoming["second"],operator=jsonIncoming["operator"])
        calculation.save()
        response = {"status": "1", "message": ("OK"), "type":"post"}
        return HttpResponse(json.dumps(response), content_type="application/json")
Denis Cornehl

The last line in your settings.py (django_heroku.settings(locals())) overrides the DATABASES setting with what is in your heroku configuration.

Change it to django_heroku.settings(locals(), databases=False) and it will work.

Additionally: you shouldn't hardcode your production configuration in your settings.py file. That's what's environment variables are for. The Mongodb-Addon you are using will add the credentials to the configuration automatically.

You may check heroku config output, or the heroku dashboard.

Heroku itself tries to follow the 12 factor app principles and so should you if you are hosting on it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

django.db.utils.ProgrammingError: column am.amcanorder does not exist

django.db.utils.ProgrammingError: Could not load : column "" of relation "" does not exist

django.db.utils.ProgrammingError: type "raster" does not exist

django.db.utils.ProgrammingError: relation "..." does not exist

django.db.utils.ProgrammingError: relation does not exist with recursive model

Django - ProgrammingError - column does not exist

Django test fails with 'django.db.utils.ProgrammingError: relation "django_content_type" does not exist'

django.db.utils.ProgrammingError: relation "django_content_type" does not exist

Django Rest Framework "django.db.utils.ProgrammingError: relation "patient" does not exist"

"django.db.utils.ProgrammingError: relation "auth_user" does not exist" Django V2.0

django.db.utils.ProgrammingError: type "int4range" does not exist

django.db.utils.ProgrammingError: relation "bot_trade" does not exist

One more "django.db.utils.ProgrammingError: relation "device_gclouddevice" does not exist"

django.db.utils.ProgrammingError: relation "company_company" does not exist when running makemigrations

django.db.utils.ProgrammingError: Table doesn't exist

django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "products_customer"

Many-to-many relationship in Django "ProgrammingError: column shoe_size_run.id does not exist"

django.db.utils.ProgrammingError: (1146, "Table 'med_portal.Custparent' doesn't exist")

django.db.utils.OperationalError: FATAL: role "django" does not exist

django.db.utils.ProgrammingError: column "image" of relation "choices_keyword" already exists

django/mysql table not found - django.db.utils.ProgrammingError: (1146, "Table 'trustline.authentication_user' doesn't exist")

psycopg2.ProgrammingError: column does not exist

ProgrammingError: relation "django_session" does not exist

ProgrammingError: relation "django_site" does not exist

django.db.utils.ProgrammingError: relation already exists on OenBSD vps

django.db.utils.ProgrammingError: syntax error at or near "FUNCTION"

django.db.utils.ProgrammingError: BLOB/TEXT column 'Thing' used in key specification without a key length - but i have no "thing" in my model

mongoDB django _id field instead of id using djongo

Connecting Django to Postgres: django.db.utils.OperationalError: FATAL: database "DATABASENAME" does not exist

TOP Ranking

HotTag

Archive