Michael Bayer wrote:
why don't you create a ForeignKeyConstraint that only has the "name" field ?   
DropConstraint doesn't need anything more than just the name.

'cos I don't know what I'm doing ;-)

also here, again we only need a name. Make yourself a fake table:

t = Table(table_name, metadata, Column('dummy', Integer))

Why the column?

This doesn't make sense, assuming inspector.get_table_names() only returns each 
table name once... Which it does, right?

that's because you're reflecting the tables, which also reflects their related 
tables.

Ah, okay. I told you I didn't know what I was doing ;-)

Anyway, new version:

    conn = engine.connect()

    # the transaction only applies if the DB supports
    # transactional DDL, i.e. Postgresql, MS SQL Server
    trans = conn.begin()

    inspector = reflection.Inspector.from_engine(engine)

    # gather all data first before dropping anything.
    # some DBs lock after things have been dropped in
    # a transaction.
    metadata = MetaData()

    tbs = []
    for table_name in inspector.get_table_names():
        fks = []
        for fk in inspector.get_foreign_keys(table_name):
            if not fk['name']:
                continue
            fks.append(
                ForeignKeyConstraint((),(),name=fk['name'])
                )
        t = Table(table_name,metadata,*fks)
        tbs.append(t)
        for fkc in fks:
            conn.execute(DropConstraint(fkc,cascade=True))

    for table in tbs:
        conn.execute(DropTable(table))

    trans.commit()


Passes tests with MySQL and SQLite. I'd still love to know why the "if not fk['name']" is needed for SQLite...

Shall I update the recipe with the above?

Chris

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" 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/sqlalchemy?hl=en.

Reply via email to