Forums

Accessing wrong database

Hi, I got 2 python apps with the same git code base and 1 of them keeps giving a 500 Server error if you try and do anything that accesses the database. My settings.py file contains this database.

DATABASES = {
241         'default': {
242             'ENGINE': 'django.db.backends.mysql',
243             'NAME': 'conceptpyrota$customerservice_20220628',
244             'USER': 'conceptpyrota',
245             'PASSWORD': xxxxxxx',
246             'HOST': 'conceptpyrota.mysql.pythonanywhere-services.com',
247             'OPTIONS': {
248                 "init_command": "SET sql_mode='STRICT_TRANS_TABLES'",
249             }
250         }
251     }

I can verify this 100% as I do a print and when i do a python manage.py command, it displays the database as above. However, in the server error log, it seems to be trying to access a different, older database, which has been deleted.

django.db.utils.OperationalError: (1049, "Unknown database 'conceptpyrota$callcenter_20220616'")

This is the previous database I was using, now deleted. What is going on here? Is there some kind of caching issue? I tried deleting the settings.pyc file and it didn''t do anything. So at the moment I have an app that fails to work, except show me the landing page.

Any suggestions?

Edit: Just to add to my own post, it seems the html being served is not the same as in the folder. I even looked with vi and it's different. So it seems the web app is using some type of cache or something is really wrong. But I can't tell.

Did you reload your web app after making changes to the settings?

Yes, a number of times.

Then you have some other code that is actually what your web app is running for the settings that is different to the code that you've been editing. Check which settings your web app is actually using.

I found the issue!. I moved the app to a different folder weeks ago and moved everything accordingly. What I failed to change was the correct folder in the WGSI file. I only found this when I deleted the entire web app and set it up again from scratch. I'm surprised the app was even running at all from a folder (at least one I can't see) for weeks. No wonder I was banging my head on the wall. An error such as "file not found" would have made this so much easier to diagnose for me, instead of running from a code base/database that no longer exist. Just posting this to help anyone else that might make the same error.

If the file that your wsgi was pointing to was not there, then your web app would not have worked after being reloaded. The reload process needs to import the settings from the file you specify and, if it is not there, the reload will fail and the web app will not start up.

I am having a similar issue: I had 2 settings files — one for development, called 'settings_dev.py', and one for production use on pythonanywhere, called 'settings_prod.py'. I've just discovered that, even though my WSGI file reads:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dynamist.settings_prod')

... PythonAnywhere is still somehow pointing to settings_dev. When I deleted settings_dev.py from the PA filesystem, I got this error (after attempting to run a Django migration on a new database):

ModuleNotFoundError: No module named 'dynamist.settings_dev'

From the traceback, I cannot figure out where the code is that is pointing to 'dynamist.settings_dev'. I have reloaded my web app several times. The WSGI seems to be configured properly: when I click the WSGI configuration file link on the Web page, it opens the correct wsgi.py file, which contains the correct settings file definition (above).

Where else might the PA system point to the incorrect settings file? Is this cached somehow? If so, how can I delete the cache?

... (continued from previous post) ... here is the full contents of my wsgi.py file, if it helps. I am wondering if not defining the path is hamstringing me here:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dynamist.settings_prod')

application = get_wsgi_application()

Ok, I figured out the issue because of this StackOverflow post: https://stackoverflow.com/questions/66619348/django-keeps-using-the-wrong-project-settings-file

The problem was that my manage.py file was setting the default settings file as well:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dynamist.settings_dev')

... I've modified this and things seem to be working.

Glad to hear that you made it work!