Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clear data for all tables via flask-sqlalchemy
def clear_data(session):
meta = db.metadata
for table in reversed(meta.sorted_tables):
print 'Clear table %s' % table
session.execute(table.delete())
session.commit()
@sfsekaran

This comment has been minimized.

Copy link

commented Nov 14, 2017

Worked great for me. Thanks!!

@alxvallejo

This comment has been minimized.

Copy link

commented Dec 19, 2017

I'm not an expert but why not just..
db.reflect()
db.drop_all()

@Jitsusama

This comment has been minimized.

Copy link

commented Feb 15, 2018

@alxvallejo; your commands would delete the tables with the data. @vkotovv gave an example of maintaining the tables while deleting their data.

@ashutosh-sharma

This comment has been minimized.

Copy link

commented Feb 16, 2018

this will only truncate the table or it will delete the tables also?

@laalaguer

This comment has been minimized.

Copy link

commented Apr 10, 2018

@ashutosh-sharma

Truncate.

db.drop_all() == "DROP TABLE xx"
table.delete() == "TUNCTATE TABLE xx"

@roelzkie15

This comment has been minimized.

Copy link

commented Aug 23, 2018

How to bypass IntegrityError when truncating data?

@mchesler613

This comment has been minimized.

Copy link

commented Feb 25, 2019

What is the type for the db object?

@bc291

This comment has been minimized.

Copy link

commented Feb 26, 2019

It depends. If using flask_sqlalchemy, then it is: <class 'flask_sqlalchemy.SQLAlchemy'>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.