Home > Software engineering >  Drop a table in django with id
Drop a table in django with id

Time:06-20

I need to drop a table in django using a model in code. After dropping a table, id of each record should start from 1. Is it possible?

CodePudding user response:

The closest you'll get with the ORM is

Model.objects.all().delete()

replace Model with your model name

or execute SQL query in database direct use truncate:

Truncate table XXX

The table handler does not remember the last used AUTO_INCREMENT value, but starts counting from the beginning. This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.

CodePudding user response:

You need to truncate() table. Not sure how it going in python, but in mysql it will be a

TRUNCATE TABLE table_name

u can find table name in model._meta.db_table

  • Related