Reputation: 137
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
Upvotes: 8
Views: 7917
Reputation: 5191
Use the ALTER SEQUENCE
command to modify PostgreSQL sequences. There is no built-in SQLAlchemy function for this, use session.execute
to execute the SQL.
db.session.execute("ALTER SEQUENCE Cache1_id_seq RESTART WITH 1")
db.session.commit()
Upvotes: 12