Python, Technical

Notes on Porting a Django App from SQLite to MySQL

This was painful. I’ll be doing this again next week, then hopefully never again.

First off, if you ever hit an error, drop and re-create your MySQL database, or at least drop the tables, or things just get weirder.

Grab the data from SQLite:

python manage.py dumpdata > $APPNAME.json

The, update settings.py to connect to the MySQL database, and if you are really lucky:

python manage.py syncdb
python manage.py reset contenttypes
python manage.py loaddata $APPNAME

(Thank you, Carl Meyer!)

Now, here’s the cute things I ran into. I had originally built a model with a TextField primary key. This is fine by SQLite but when Django tries to create a BLOB field you get in trouble asking for it to be unique, never mind a primary key. Fortunately, it was easy enough for me to simply change it to a CharField, which will tell Django to use VARCHAR. (SQLite certainly didn’t mind.)

The other was that neither Django nor SQLite were enforcing field length limitations, so I would hit some errors when loaddata tried to bring in database entries that were too long. I worked around this by raising my length limitations. There was also some ugliness with a UTF-8 string, which I solved by creating the text object in question.

Read More

Categories: Python, Technical

Discover more from dannyman.toldme.com

Subscribe now to keep reading and get access to the full archive.

Continue reading