Adding InnoDB to sqlall output
Sunday, March 23rd, 2008Have you ever wondered why you can’t have manage.py sqlall output your create table statements as InnoDB tables for mysql? Well I have. I always go in and update the statements by hand to add that after the fact. So I did a bit of pokeing around and figured out a hack to add in a settings flag so you can set the table type that your manage.py will use for its create table statements.
There are 2 changes you need to make. First off you need to add the following setting in your settings.py
DATABASE_TABLE_ENGINE = 'InnoDB'
the other part is an update to the django.core.management.sql file. This file is really long (even though the actual hack is short). Basically the update is adding the following code in the many_to_many_sql_for_model and sql_model_create functions.
try:
if settings.DATABASE_TABLE_ENGINE != None:
table_output.append('ENGINE=%s' % settings.DATABASE_TABLE_ENGINE)
except:
pass
It’s really pretty basic but personally I find it useful. Now (here comes the disclaimer) I can’t guarantee that there isn’t already a way to do this. Despite all my digging I didn’t find it if it does exist. Also I’m sure there is a better way to add this update then going in and hacking the core script (or even if that’s playing nice and submitting an enhancement ticket which I should really read up on). On that note I would suggest not just swapping out the download version for your version. It would prob be best to just use my version as a guide to show you how to update your own sql.py so you don’t run into any file version probs. Just for the sake of clarity I’m running django svn trunk version 6898.
Edit I noticed my local django version was pretty damn old so I just updated to 7353. The hack still seems to be working fine. End Edit
You can download a copy of my hacked sql.py here: updated django.core.management.sql . As always use at your own risk and let me know what could be done better.
Edit I’m adding a svn diff as well for those of you who would rather see only the changes: download diffEnd Edit