makemigrations and migrate commands are ncessary to add things to your database from your models.py files, this tutorial will take you through what happens during the migration process.
Your database, through SQLite by default, will be updated like this > all of your apps will have their model like appname_modelname.
Every time you migrate or make migrations, a file is created in the migrations folder of your apps.
The tables automatically added at the first migrate are necessary for running Django, like the user model that is used for superusers and admins.
Django knows what tables to create right off the bat because of the installed apps on settings.py:
this is one reason why we must put all of the apps we create into this list.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
auth_permissions gets automatically filled with a number of permissions for each app’s models.
add, change, view, delete for each one.
django_admin_log will automatically store everything done in the admin panel.
django_content_type shows every model and what app they belong to.
django_migrations shows a record of all the migrations that have been made so far onto the database.
This is almost like a version control that allows us to track and roll back changes.
django_session shows all active sessions on your site, for example the only one in this image is the admin user on local host.
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Post(models.Model):
category = models.ForeignKey(
Category, on_delete=models.PROTECT, default=1)
title = models.CharField(max_length=250)
excerpt = models.TextField(null=True)
class Meta:
ordering = ('-title',)
def __str__(self):
return str(self.title)
We created these simple models, so let’s go ahead and migrate the data to see what happens.
If we now go into the blog folder, we see the migrations folder with a 0001_initial.py file.
# Generated by Django 4.1.7 on 2023-03-21 20:09
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Post',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=250)),
('excerpt', models.TextField(null=True)),
('category', models.ForeignKey(default=1, on_delete=django.db.models.deletion.PROTECT, to='blog.category')),
],
options={
'ordering': ('-title',),
},
),
]
When we create a new model, we don’t specify an ID, it’s just added automatically to be used as the primary key.
One the most powerful tools in Django is the object relational mapper, which allows us to edit the database without having to write any SQL ourselves whatsoever.
another useful command for migrating is py manage.py showmigrations
Items with an [X] represent migrations that have been committed, while [ ] represents unfinished migrations. You have to run the migrate command in order to commit a migration that was set up with makemigrations.