Resetting DB in Django

Learn via video courses
Topics Covered

Overview

Django is a high-level Python web framework that allows the rapid development of secure and maintainable websites. It is free and open source. Django supports some of the standard Database programs like PostgreSQL, SQLite, MySQL, etc. Resetting DB is the process of removal of all the data from the database tables.

Introduction

In our daily life, you must have come across a situation where you want to reset and give it a fresh start. Well, these situations may arise when perhaps we ran some migrations that we don't intend to keep anymore as in the data we entered that is of no use anymore, or maybe we want to let go of some test data. Possible causes might also include the addition or removal of some database tables, modifications in database design, concerns with logic and relationships, or the database becoming overpopulated with useless data. Django reset database commands reset a specific database's configuration to the system defaults. Django makes it simple for users to reset their databases.

Manually Resetting Database

Django resetting of the database can be done manually by following the steps given below. There are four steps involved:

Delete all migrations files

Migrations are Django's method of propagating changes made in the model (adding a field, deleting a model, etc.) into our database. Django's migration can be reset by deleting all migration files such as 0001_inital, 0002_delete_song, 0003_delete_album, etc. except the __init__.py files in each project app directory, then dropping the database and creating migration again.

Delete db.sqlite3 file

After clearing the migration files, our next step is to delete the db.sqlite3 file. SQLite is an embedded database engine, so deleting a database requires deleting the file from the machine. db.sqlite3 is a database file that will keep all of the data we generated in our project.

Make new migrations files

Use the following command to migrate all Django applications.

In some cases, migrations are not executed for the applications. In such cases, we add the application names along with the following command to migrate all the changes in the respective applications.

Migrate changes

In the end, we migrate changes creating a new db.sqlite3 database, using the following command.

reset_db in Django

So apart from resetting the Django database manually by following the steps, we can also reset it using the command reset_db. This command clears all data from all tables in our Django database. This command deletes and recreates the database of our Django app. By default, the command will ask for permission to delete the data. This can be avoided with the help of an additional command --noinput.

The following command resets the database so that no data is present and migrations can be run again.

We can avoid the confirmation message that asks for permission to delete the data, we write,

This command enables us to use a username and password other than specified in the settings.py file.

Flush Command to Reset the Database

There is another command which can help us reset our database in Django apart from following the manual method, we can also use a command known as a flush command. The flush command removes all the database data and re-runs any post-synchronization handlers.

The above command does not migrate changes but only deletes the data. Therefore, we are supposed to makeigrations and then migrate using the following commands.

Migrate Zero Method

Migrations zero refers to a set of migrations that are required and sufficient to reflect the database as it must be (without any data, and without anything to migrate from). The migrate zero method is more closely associated with Django applications. It can undo all Django migrations related to a specific application. It rebuilds models from a given application, resulting in new tables with no old data.

To use this command we write the name of the app followed by zero.

Conclusion

Hello Developer! By now you must have got an idea about resetting a database in Django. Let us summarize what we have learned so far

  • Django supports some of the standard Database programs like PostgreSQL, SQLite, MySQL, etc.
  • We can reset the Django database either manually by deleting the files and making migrations or we can also do it using some special commands.
  • reset_db command deletes and recreates the database of our Django app.
  • The Flush command does not migrate changes but only deletes the data.
  • The migrate zero methods is more closely associated with Django apps.