I am using a Postgres database with sqlalchemy in my Flask API, my database models are defined as follow:
class Cache1(db.Model):
__tablename__ = 'Cache1'
id = db.Column(db.Integer, Sequence('Cache1_id_seq', start=1, increment=1), primary_key=True)
name = db.Column(db.String(20))
The problem is when i do Cache1.query.delete() in one of my views and then i try to put new records in Cache1, the sequence number does not start from 1. I should always go to Postgres and do ALTER SEQUENCE "Cache1_id_seq" RESTART WITH 1.
Can someone show me a way to do this in Sqlalchemy . Thank you in adcance
TRUNCATE TABLE xxxxx RESTART IDENTITY;if that is supported by your ORM. (and take care of foreign keys referring to this table) postgresql.org/docs/9.5/interactive/sql-truncate.htmlTRUNCATE TABLE ...but after each delete of the cache table i should do it in postgres. I am looking for a way to restart the identity from sqlalchemy.db.session.execute('ALTER SEQUENCE "Cache1_id_seq" RESTART WITH 1')? There is usually no good reason you would ever need to reset a sequence, though. Usually it's for something unimportant, like cosmetic reasons.