Created
October 23, 2013 12:39
-
-
Save chappyhome/7117899 to your computer and use it in GitHub Desktop.
Move django from SQLite to MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This was extremely helpful. However, I had to make one modification for this to work.
If your tables have foreign key constraints they must be turned off before you can run the import script.
So, you list should have
run mysql as root
SET GLOBAL FOREIGN_KEY_CHECKS=0;
before running script.
run mysql as root
SET GLOBAL FOREIGN_KEY_CHECKS=1;
after running script.