Chapter 2. Django Quick Start

Database Migration

Database Migration
Tag:

As a default, Django provides basic database designs used for admin and user authentication and some other basic features. As a default, the database design has not yet been reflected in the Django database. This relates to the alert you saw when you ran the runserver command in the previous topic. In order to reflect the prepared database design, you need to migrate it into the database.

The migrate command

There are three steps to update a database used in Django.

  1. Edit models.py file: create or modify Models
  2. Run the makemigration command: create a migration file based on the Models
  3. Run the migrate command: create tables in a database based on the migration file

As we haven't touched models.py, we'll skip the first two steps in this topic. We'll cover those two steps in detail later.

Just for checking purposes, run the makemigration command. You'll see that nothing happens.

Command Line - INPUT
python manage.py makemigrations
Command Line - RESPONSE
No changes detected

However, there are several migration files already prepared for basic Django features. By running the migrate command, Django creates tables in a database using the preset migration files. The migration command is executed using the manage.py file like the shown below.

Command Line - INPUT
python manage.py migrate

When you run the command above, you'll see that migrations are applied to several migration files.

Command Line - RESPONSE
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK

When you execute the runserver command, you won't see the alert shown in the previous topic anymore.

Command Line - INPUT
python manage.py runserver
Command Line - RESPONSE
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 19, 2023 - 02:40:30
Django version 4.1.7, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

IdeaNote: Default Database for Django

The default database for Django is SQLite, which is open-source free database software. It is a lightweight, server-less database mainly used in small to medium-scale applications. It is better to use other databases, such as MySQL or PostgreSQL, for a large-scale application. To change the default database, you need to modify the settings.py file. We'll explain how to modify it in a later part of this course.

As a default, Django provides basic database designs used for admin and user authentication and some other basic features. As a default, the database design has not yet been reflected in the Django database. This relates to the alert you saw when you ran the runserver command in the previous topic. In order to reflect the prepared database design, you need to migrate it into the database.

The migrate command

There are three steps to update a database used in Django.

  1. Edit models.py file: create or modify Models
  2. Run the makemigration command: create a migration file based on the Models
  3. Run the migrate command: create tables in a database based on the migration file

As we haven't touched models.py, we'll skip the first two steps in this topic. We'll cover those two steps in detail later.

Just for checking purposes, run the makemigration command. You'll see that nothing happens.

Command Line - INPUT
python manage.py makemigrations
Command Line - RESPONSE
No changes detected

However, there are several migration files already prepared for basic Django features. By running the migrate command, Django creates tables in a database using the preset migration files. The migration command is executed using the manage.py file like the shown below.

Command Line - INPUT
python manage.py migrate

When you run the command above, you'll see that migrations are applied to several migration files.

Command Line - RESPONSE
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK

When you execute the runserver command, you won't see the alert shown in the previous topic anymore.

Command Line - INPUT
python manage.py runserver
Command Line - RESPONSE
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 19, 2023 - 02:40:30
Django version 4.1.7, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

IdeaNote: Default Database for Django

The default database for Django is SQLite, which is open-source free database software. It is a lightweight, server-less database mainly used in small to medium-scale applications. It is better to use other databases, such as MySQL or PostgreSQL, for a large-scale application. To change the default database, you need to modify the settings.py file. We'll explain how to modify it in a later part of this course.

Tag: