How can I change a Django db to MySQL on pythonanywhere?

2.1k views Asked by At

I thought this would be simple because of this MySQL tutorial by pythonanywhere, but I'm still having trouble switching over from sqlite3. I'm a beginner to SQL databases, and I've been checking out other stackoverflow questions but I'm not sure where else to go from here. Here's what I've done so far.

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '3DPrince$ubodb',
        'USER': '3DPrince',
        'PASSWORD': 'secretpassword',
        'HOST': '3DPrince.mysql.pythonanywhere-services.com',
    },
}

I've also run the following to try and sync the db.
manage.py makemigrations
manage.py migrate

I'm still getting the error that
(1146, "Table '3DPrince$ubodb.django_site' doesn't exist")
I'm not sure what else to do from here and I'm not sure how to do any sort of checks from the mysql bash console.

Can anyone point out what I'm doing wrong? Or maybe some useful mysql bash commands to check the connection or manually remake the db?

2

There are 2 answers

2
Fomalhaut On BEST ANSWER

It looks like something went wrong with the migration. I would recommend you to do following steps that re-create your db.

  1. Make a backup of your data in the database!!!

  2. Connect to your remote database:

    $ mysql -h 3DPrince.mysql.pythonanywhere-services.com -u 3DPrince -p
    
  3. Delete your current database and create it again:

    drop database `3DPrince$ubodb`;
    
    create database `3DPrince$ubodb`;
    
  4. Locally (in another terminal's tab) migrate your django project:

    $ python manage.py migrate
    
  5. Check (in the tab where you're connected to the remove database) that all tables have been created correctly:

    use `3DPrince$ubodb`;
    
    show tables;
    

If something goes wrong you will see warnings or errors.

And also you don't need to do python manage.py makemigrations all the time because this command only creates migration files. (they are stored in yourproj/yourapp/migrations) and does nothing towards the interaction with the real database. If you don't modify your project you don't actually need to re-create the migration files.

0
RknRobin On

The steps above helped me debug my error. It turns out my backend was still recognizing the previous settings file I had used, while my front end was looking for the new MYSQL one that I switched to for production. All I did was comment out the DATABASES from the development settings.py file and migrate again and it worked.