DEV Community

fiona
fiona

Posted on • Edited on

4 1

Django: switch from sqlite3 to mysql

assume there is an existing sqlite3 db with data in django app

in command prompt:

python manage.py dumpdata > datadump.json
# to export existing data into a json file

create a new schema in mysql server
(mysql should have been installed already)
for my own information: Difference Between Schema / Database in MySQL

in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<schema_name>',
        'USER': 'root',
        'PASSWORD': '*****',
        'HOST': 'localhost',
        'PORT': '<port_num>',
    }
}

in command prompt:

pip install mysqlclient
# mysqlclient is a fork from MySQLdb1 which supports python3
# both are MySQL database connector for Python

more info: What's the difference between MySQLdb, mysqlclient and MySQL connector/Python?

then makemigrations, migrate and finally,

python manage.py loaddata datadump.json
# should return sth like "Installed 59 object(s) from 1 fixture(s)"

done.

problem shooting (not ideal but smh works)

  • delete the files in 'migrations' folder (except for __init__)
  • "duplicate entry" -> drop the schema in mysql and create a new one

reference

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more