PostgreSQL – Reset ID count after table deletion ()

For testing purposes, I clear (delete) each table before executing the code.

for table in reversed (db.metadata.sorted_tables):
engine.execute(table.delete())

do_stuff()

However, the id value of the new data is from the previous id The stop position starts:

First iteration:

id | value 
-----+------ ---
1 | hi
2 | there

The second iteration (delete table, insert new data):

id | value 
-----+---------
3 | good
4 | day

Did I delete the table? How to reset the id count?

Edit: It seems that I broke it and it was not cleared 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 the terminal and When directly executing TRUNCATE TABLE myTable RESTART IDENTITY CASCADE, it works.

table.name gives the correct name. This leads me to believe that there is a problem with db.engine.execute(), but because of db.session .add_all() doesn’t make much sense

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

For testing purposes, I clear (delete) each table before executing the code. < p>

for table in reversed(db. metadata.sorted_tables):
engine.execute(table.delete())

do_stuff()

However, the id value of the new data is stopped from the previous id Start:

First iteration:

id | value 
-----+---------
1 | hi
2 | there

The second iteration (delete table, insert new data):

id | value 
-----+---------
3 | good
4 | day

Is there any way to reset when I delete the table id count?

Edit: It seems that I broke it and it was not cleared 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 the terminal and When directly executing TRUNCATE TABLE myTable RESTART IDENTITY CASCADE, it works.

table.name gives the correct name. This leads me to believe that there is a problem with db.engine.execute(), but because of db.session .add_all() doesn’t work much.

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

Leave a Comment

Your email address will not be published.