adding stored procedures / postgres functions to db?

306 views
Skip to first unread message

mazurin

unread,
May 10, 2006, 9:20:10 AM5/10/06
to Django users

Hi,

I'm trying to implement a complicated sequence of updates inside a
form-handling view, and I realized that it might be best to do it
inside a Postgresql function. In fact, I've already found sample code
for a postgres function that's almost exactly what I need.

My question is, how do I add the CREATE FUNCTION statement to
Django's db-creating process? For initial data, I know that I just make
a bunch of .sql files that have the same name as the models, and put
them inside the sql/ directory. Is there something similar I can do for
functions?

Thanks! I hope this isn't a dumb question.

Mikah

--

Malcolm Tredinnick

unread,
May 10, 2006, 9:33:16 AM5/10/06
to django...@googlegroups.com

All of the initial data is inserted after all the tables have been
created. So you can put your stored procedure creation code in the
initial SQL data file for (any) one of your models. There is no generic
initial data file that is read, so this would seem to be the simplest
solution.

Regards,
Malcolm

Adrian Holovaty

unread,
May 10, 2006, 10:53:26 AM5/10/06
to django...@googlegroups.com
On 5/10/06, mazurin <maz...@gmail.com> wrote:
> My question is, how do I add the CREATE FUNCTION statement to
> Django's db-creating process? For initial data, I know that I just make
> a bunch of .sql files that have the same name as the models, and put
> them inside the sql/ directory. Is there something similar I can do for
> functions?

Hi Mikah,

Those .sql files that you put in the sql/ directory can contain
arbitrary SQL, including CREATE FUNCTION statements. So go right ahead
and put your function(s) in there.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

mazurin

unread,
May 11, 2006, 2:29:10 AM5/11/06
to Django users
Malcolm and Adrian,

Thanks so much for your quick replies! I thought of that, but I
didn't know if it was the Djangoic way of doing it.

Follow up question -- are _all_ the files in the sql/ directory
executed, or just those that have the same names as the models?

mikah

--

Malcolm Tredinnick

unread,
May 11, 2006, 2:52:47 AM5/11/06
to django...@googlegroups.com
Hi Mikah,

On Wed, 2006-05-10 at 23:29 -0700, mazurin wrote:
> Malcolm and Adrian,
>
> Thanks so much for your quick replies! I thought of that, but I
> didn't know if it was the Djangoic way of doing it.

You're welcome. :-)

> Follow up question -- are _all_ the files in the sql/ directory
> executed, or just those that have the same names as the models?

Just those that match the model class names. And to head off a related
question: the order they are executed in is, as far as I can see,
arbitrary; the only guarantee is that all the tables for all the models
will already have been created.

Regards,
Malcolm

mazurin

unread,
May 11, 2006, 3:33:04 AM5/11/06
to Django users
Hi Malcolm,

Got it. That's all I need to know.

Thanks!

Mikah

--

Reply all
Reply to author
Forward
0 new messages