Skip to content

Instantly share code, notes, and snippets.

@cvam0000
Forked from chappyhome/gist:7117899
Created September 6, 2018 05:06
Show Gist options
  • Save cvam0000/1d65d6158cf67559a2b9f73e1f209cfb to your computer and use it in GitHub Desktop.
Save cvam0000/1d65d6158cf67559a2b9f73e1f209cfb to your computer and use it in GitHub Desktop.
Move django from SQLite to MySQL
When you start develop new site, you may run from little one with tiny database.
For django best start from sqlite and after some time, if you project begin grow you move to more serious database engine (MySQL, Postgre SQL etc).
Sadly, django does not have any ready tools to move from one to another database.
You may try
python ./manage.py dumpdata > data.json
and
python ./manage.py loaddata data.json
but unfortunately this doesn't work because of integrity errors...or many other issues
For move from one db to another i follow steps:
Edit settings.py
add new database in DATABASES settings with 'slave' name like:
DATABASES = {'default': {...}, 'slave': {...}}
if you use apps like south - comment them for a while
Create new db (django create only new file for sqlite database type)
Create tables in new db
python ./manage.py syncdb --database slave
enable south and repeat 3.1
Create file to_slave.py in main project directory:
from django.contrib.contenttypes.models import ContentType
def run():
def do(Table):
if Table is not None:
table_objects = Table.objects.all()
for i in table_objects:
i.save(using='slave')
ContentType.objects.using('slave').all().delete()
for i in ContentType.objects.all():
do(i.model_class())
If you have any kind of signals for you models - disable them before next step
run
python ./manage.py shell and type command in shell
from to_slave import run
run()
Make 'slave' default database in you project
Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment