It's generally a problem with constraints.  The correct way to do it
is to create all your tables, then alter the tables to add in the
constraints.  Quite often, if you have two tables that each refer to
the other, there is no order you can put them in that works, without
moving the constraints out.

I wrote a Textmate command to do this automatically for me.  It takes
the output of tg-admin sql sql and moves all constraints to the end.
It isn't very robust so the sql syntax will need to be precise, and
unfortunately tg-admin sql sql often returns broken sql for postgres.
There is quite often a missing semi-colon at the end of statements, so
you HAVE to sanity check your sql before running it through this.

########
#!/usr/bin/env python

import sys, re

sql = sys.stdin.read()

create_rgx = re.compile("""(CREATE TABLE (\w*)\s+([^;]*);)""")

contstraint_rgx = re.compile("""(?s)(,\s+(CONSTRAINT.*?))(,|\);)""")


creates = create_rgx.findall(sql)

for create in creates:
    constraints = contstraint_rgx.findall(create[0])
    if constraints:
        constraint_list = ["\tADD " + constraint[1] for constraint in
constraints]
        constraint_sql = "ALTER TABLE " + create[1] + "\n" +
",\n".join(constraint_list) + ";\n\n"
        for constraint in constraints:
            sql = sql.replace(constraint[0], '', 1)
        sql = sql + constraint_sql

print sql
##############

On 3/11/07, Johnny Blonde <[EMAIL PROTECTED]> wrote:
>
>
>
> On 11 Mrz., 17:44, "Oleg Deribas" <[EMAIL PROTECTED]>
> wrote:
> > There is problem with PostgreSQL. "tg-admin sql create" command
> > creates tables in wrong order. As quick hack you could define creation
> > order in your model using soClasses variable:
> >
> > soClasses = ('Class1', '','Class2', 'Class3')
> >
> > But if you want to use "tg-admin sql drop" you'll need to define
> > soClasses in reverce order ;-)
>
> have had the same error with postgre.
>
> i did tg-admin sql sql > out.sql, rearragned the tableorder and posted
> it to pgadmin, it then worked just fine.
>
> does anybody know if they work on it to fix the error?
>
> Frank
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to