Django "DATABASES is improperly configured" error after updating versions

212 Views Asked by At

I am trying to update a few-year old existing django project to current django and python 3. We're running into a multitude of issues, but most haven't been to hard to identify and resolve. Currently we are running into the dreaded django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. error when trying to manage.py migrate or some other commands like inspectdb.

The odd thing is that we definitely are defining the database config with a default db and engine. This setup below is from our settings.py file. It's set up that way just so that we could use sqlite for testing purposes or mysql like the live servers. I've tried both, and it doesn't work with either. I have put print statements in to confirm that it does hit the appropriate sections. I also tried the dummy engine just to test and that had the same issue.

#LOCALDEV_DB = 'MYSQL'
LOCALDEV_DB = 'SQLITE'
if LOCALDEV_DB == 'MYSQL':
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'foo',
            'USER': 'root',
            'PASSWORD': '123456789',
            'OPTIONS': { 'init_command' : 'SET default_storage_engine=MYISAM', },
            #'STORAGE_ENGINE': 'MYISAM'
        }
    }

elif LOCALDEV_DB == 'SQLITE':
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

The really odd thing is that when I do manage.py diffsettings it does in fact show that the database seems to be set up correctly:

DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'C:\\Users\\path\\to\\base\\folder\\db.sqlite3'}}

So essentially, I'm looking for any guidance on this. I've read through dozens of similar errors on SO and around the internet, and usually it's just an issue of not having a default DB or the engine defined. I seem to have both... is there something that I have forgotten or that's changed in django config in the past few years? We're jumping forward quite a few versions, so I'm hoping that I'm just missing some dumb difference in configuration in all of the documentation that I've been reading through.

Edit: I have a tentative fix but I don't think it is legit enough to consider an actual answer. I had issues with logging initially and many solutions to the error I was getting recommended putting in a django.setup() call in the settings (though there were also people saying that this shouldn't remain in for actual deployment). That ended up fixing it that issue but eventually I ended up with this current issue. I just moved the django.setup() call beneath the database stuff, just above the logging, and now I was able to fully migrate. Any ideas why that's "fixing" stuff and what I might be able to do to avoid it?

0

There are 0 best solutions below