Forums

Can't connect MySQL database with Django application

Hello,

I just transitioned my django application from sqllite to MySQL. My setting file now looks like this

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "fstg$fstg",
        "USER": "fstg",
        "PASSWORD": "*********",
        "HOST": "fstg.mysql.pythonanywhere-services.com",
    }
}.

When I try to run the migrations on a console though it says there is no migration to apply. But when I connect via a shell to my new MySQL DB it says there is no table in the DB.

Also, when starting the application, I get an error

django.db.utils.ProgrammingError: (1146, "Table 'fstg$default.django_session' doesn't exist")

It seems to me that my Django app is not considering the correct database (maybe still creating a sql lite DB) when trying to run the migrations.

Can anyone help? :)

Thanks!

Do you have multiple settings files? Do you run migrate with the correct one?

You are genius! Yes I have one for local development and the other one for production. I tried to set the Mysql for all settings and it works thank you very much!

Can you explain me why it is my dev setting file which is taken into account by PythonAnywhere?

My WSGI says it should be the production one. Here is the code inside

import os
import sys

path = '/home/fstg/from_sand_to_green'  # Le dossier qui contient manage.py
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'from_sand_to_green.settings.prod'  # Indiquez le dossier qui contient le fichier settings.py

# then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Thank you so so much!

Can you explain me why it is my dev setting file which is taken into account by PythonAnywhere?

Looks like you were not pointing to them when running migrate. Management commands are not running in the environment where DJANGO_SETTINGS_MODULE variable set in wsgi file is present .