For testing purposes, I delete (delete) each table before executing the code.
for table in reversed(db.metadata.sorted_tables): engine.execute(table.delete()) do_stuff()
However, the new id data values ββstart from where the previous id was stopped:
First iteration:
id | value -----+--------- 1 | hi 2 | there
Second iteration (delete table, insert new data):
id | value -----+--------- 3 | good 4 | day
Is there any way to reset id count when deleting a table?
EDIT: it seems I broke it, the table doesn't clear at all
config.py SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:myPassword@localhost/myDatabase' app.py app = Flask(__name__) app.config.from_pyfile('config.py') db = SQLAlchemy(app) models.py from app import app, db def clear(): for table in reversed(db.metadata.sorted_tables): db.engine.execute('TRUNCATE TABLE ' + table.name + ' RESTART IDENTITY CASCADE')
The table is still being added (using db.session.add_all() and db.session.commit() ). However, clear() does nothing. When I log in as the postgres user in terminal and directly execute the TRUNCATE TABLE myTable RESTART IDENTITY CASCADE , it works.
table.name gives the correct names. This makes me think that something is wrong with db.engine.execute() , but that doesn't make much sense, since db.session.add_all() works
postgresql flask-sqlalchemy sqlalchemy
onepiece Apr 13 '14 at 2:23 2014-04-13 02:23
source share