Creating my tables from my models.py. I donno how to do 2 things –
- I want to specify
MySQLto create some of my tables asInnoDB& some asMyISAM. How do I do it? - Also I want to specify my tables
DEFAULT CHARSETasutf8. How do I do it?
This is what I see when I run syncdb –
...
) ENGINE=MyISAM DEFAULT CHARSET=latin1
I use Ubuntu 10.04, Django 1.2.X, MySQL 5.1.X
UPDATE:
I thought these might be MySQL default settings & I ended up changing my.cnf where I added default-character-set = utf8. But to no use.
I don’t think you can change storage engines on a table-by-table basis, but you can do it on a database-by-database basis. This, of course, means that InnoDB foreign key constraints, for example, can’t apply to foreign keys to MyISAM tables.
So you need to declare two “databases”, which may very well be on the same server:
And you’ll just need to apply
using('innodb')to querysets for tables in InnoDB land.As for UTF-8, again, I think you need to do this at the database level. I don’t think
syncdbcreates the database for you, just the tables. You should create the database manually anyway, so you can have privileges set right before runningsyncdb. The database creation command you want is:That said, I usually recommend that people create two django users in the database: one for database schema work (“admin”) and one for everything else (with different passwords):
(Note that this needs to be done for each database.)
For this to work, you need to modify
manage.py:Then in your
settings.py, use the environment variable to pick the right settings. Make sure the site (i.e. non-admin) user is the default.(Additionally, I don’t store the database setup,
SECRET_KEY, or anything else sensitive insettings.pybecause my Django project is stored in Mercurial; I havesettings.pypull all that in from an external file accessible only by Django’s user and the server admins. I’ll leave the “how” as an exercise for the reader… because I detailed some of it in answers to others’ questions, and I’m too lazy to look it up right now.)