简体   繁体   中英

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

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM