Forums

Clear SQLdatabase in python script, including IDs

How do I automatically clear a specific table in my SQLdatabase in my python script, including resetting ID numbers?

I know that you can clear a table (including resetting id numbers) from the SQLconsole via something like https://stackoverflow.com/questions/23038422/reset-id-count-after-table-delete or https://www.pythonanywhere.com/forums/topic/9312/ ;

However, these are all from the SQL console and not the python script. I wish to do it from the python script because then this process can be automatic: I can click a href link on my website and then it will clear all the data from the website, causing reloading to remove all the things created from the database. (I'm finished with this great tutorial: https://blog.pythonanywhere.com/121/ , but I want to know how to delete the comments)

Another method is my current one:

db = SQLAlchemy(app)
...
class Comment(db.Model):
...
db.session.query(Comment).delete()
db.session.commit()

Unfortunately, the unique id counter is not reset, hence my question.

Any help would be appreciated! Thank you.

SQLAlchemy is for interfacing existing tables in a database with Python code, so I doubt that they'd have a way of deleting a table. If you are concerned about what your ids are, just add an additional column that you can use as an id column that is not an autoincrementing id and initialise and increment it yourself in code. Then you can reset it whenever you like.