The right way to clear database of content?

6,090 views
Skip to first unread message

Arve Knudsen

unread,
Feb 14, 2011, 7:29:19 AM2/14/11
to sqlal...@googlegroups.com
Hi

What's the right way to clear a database all of content, but keep the schema? I tried the method of deleting all tables at http://www.sqlalchemy.org/docs/05/metadata.html#reflecting-all-tables-at-once, but content still remains. I couldn't find any documentation on Table.delete either, for that matter. Maybe I'm missing something...

Thanks,
Arve

GHZ

unread,
Feb 14, 2011, 8:51:36 AM2/14/11
to sqlalchemy
maybe it needs to be in a transaction:


con = engine.connect()

trans = con.begin()

for name, table in meta.tables.items():

print table.delete()
con.execute(table.delete())

trans.commit()



On Feb 14, 1:29 pm, Arve Knudsen <arve.knud...@gmail.com> wrote:
> Hi
>
> What's the right way to clear a database all of content, but keep the
> schema? I tried the method of deleting all tables athttp://www.sqlalchemy.org/docs/05/metadata.html#reflecting-all-tables...,

Arve Knudsen

unread,
Feb 14, 2011, 10:02:46 AM2/14/11
to GHZ, sqlalchemy
Thanks, will give this a shot.

Sent from my Windows Phone From: GHZ
Sent: 14. februar 2011 14:51
To: sqlalchemy
Subject: [sqlalchemy] Re: The right way to clear database of content?


con = engine.connect()

trans = con.begin()

print table.delete()
con.execute(table.delete())

trans.commit()

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.

Arve Knudsen

unread,
Feb 15, 2011, 7:29:41 AM2/15/11
to sqlal...@googlegroups.com
Thank you GHZ, it did work! Wondering about one thing though; the recipe in the documentation iterates over the tables in reverse sorted order, like so:

for table in reversed(meta.sorted_tables)

Do you know what this would be good for (since your code does not care about the table order)?

Arve

GHZ

unread,
Feb 15, 2011, 7:55:58 AM2/15/11
to sqlalchemy
I the order is required for Foreign Key relationships. i.e. to make
sure the children are deleted before the parents.

So the "for table in reversed(meta.sorted_tables)" example is the more
correct way to delete all data.

Arve Knudsen

unread,
Feb 15, 2011, 8:04:16 AM2/15/11
to sqlal...@googlegroups.com
Aha, thanks again! :)

Arve
Reply all
Reply to author
Forward
0 new messages