0

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?

zaknotzach
  • 977
  • 1
  • 9
  • 18
  • `'NAME': os.path.join(BASE_DIR, 'db.sqlite3'`. This should give a value of `"/some/directory/db.sqlite3"` But your output has `'NAME': 'sqlite_db'`. These do not match. Something is different from what you have shown us. – John Gordon Feb 11 '23 at 01:19
  • Sorry, that output was from when I tried changing the name to just "sqlite_db" for testing! Fixing that in the post. – zaknotzach Feb 11 '23 at 01:38
  • Can you run `python manage.py shell` successfully? If so, show us the output of this code: `from django.conf import settings; print(settings.DATABASES)` (of course remove any passwords from the output) – John Gordon Feb 11 '23 at 01:46
  • Is there anywhere in your code that uses a database other than "default"? – John Gordon Feb 11 '23 at 01:49
  • Yep, shell works. The output matches the diffsettings output: `In [1]: from django.conf import settings; print(settings.DATABASES) {'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'C:\\Users\\...\\db.sqlite3'}}` There is nowhere else that uses any other databases but default. This was working code for a few-year old version of django, and is still working code live on our server. I tried taking the conditional out as a test as well so only one database definition in the whole settings file with the same result. – zaknotzach Feb 11 '23 at 01:54
  • Could be related to using an old version of Django. Old post, but have a look at https://stackoverflow.com/q/10228618/494134 – John Gordon Feb 11 '23 at 02:01
  • So in this case I'm updating from an old version of django, something like 1.9, to current. I just double checked and my django version is 4.1.5. I'm sure it's something in the dozen or so versions between that's changed... I just haven't kept up enough with django over the past few years to know what it might be! I'm trying to read update docs and deprecation docs but nothing jumps out to me. – zaknotzach Feb 11 '23 at 03:44
  • I would start by getting a fresh settings.py file and copying your settings into that file one-by-one, instead of trusting an ancient file. – John Gordon Feb 11 '23 at 04:10

0 Answers0