Reset id count after deleting table ()

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

+3
postgresql flask-sqlalchemy sqlalchemy
Apr 13 '14 at 2:23
source share
1 answer

Call TRUNCATE TABLE MyTable RESTART IDENTITY; for each table in the loop, instead of calling table.delete() , this should reset the auto-increment sequence.

+1
Apr 13 '14 at 2:31
source share



All Articles