views:

35

answers:

1

From the documentation:

Django provides a hook for passing the database arbitrary SQL that's executed just after the CREATE TABLE statements when you run syncdb. You can use this hook to populate default records, or you could also create SQL functions, views, triggers, etc.

The hook is simple: Django just looks for a file called sql/[modelname].sql, in your app directory, where [modelname] is the model's name in lowercase.

It's not that simple. This does not seem to work when my model is declared in a sub-module of the app, and uses the app label mechanism:

in myapps/foo/models/bar.py:

class Baz(models.Model):
   ...
    class Meta:
        app_label = "foo"

Providing myapps/foo/sql/baz.sql does not work, neither does foo_baz.sql.

Any ideas?

A: 

Not sure what you want to do, but you could possibly write a management module in your app that listens for the post_syncdb signal.

But if you just want to populate the DB, you're probably better off using fixtures: http://docs.djangoproject.com/en/dev/howto/initial-data/

Chris Lawlor
Thanks, already found that workaround. Seems a bit heavy for what I actually want to accomplish: issue a simple ALTER TABLE to change the ENGINE (from InnoDB to a different one).
prometheus
Agreed, using post_syncdb seems a little over the top. Check out http://code.djangoproject.com/ticket/10985 - your custom sql needs to be in the models dir (doesn't say what the filename should be though)
Chris Lawlor
Thanks! This seems to be the solution: "notice that if you have custom SQL or fixtures they have to be placed inside the models/ directory not in your app dir as the documentation says."
prometheus