| 5 | |
| 6 | |
| 7 | def delete_all_data(): |
| 8 | meta = MetaData() |
| 9 | meta.reflect(bind=db.get_bind()) # Get the engine associated with the session |
| 10 | |
| 11 | # First, delete child table data |
| 12 | child_tables = [table for table in meta.sorted_tables if table.foreign_keys] |
| 13 | |
| 14 | for table in child_tables: |
| 15 | db.execute(table.delete()) |
| 16 | |
| 17 | # Then, delete parent table data |
| 18 | parent_tables = [table for table in meta.sorted_tables if not table.foreign_keys] |
| 19 | |
| 20 | for table in parent_tables: |
| 21 | db.execute(table.delete()) |
| 22 | |
| 23 | db.commit() |
| 24 | |
| 25 | |
| 26 | if __name__ == "__main__": |