JAEMYUN IS GRANDPA JAEMYUN IS GRANDPA - 2 months ago 16
Python Question

Django, How does models.py under auth folder create initial tables when you migrate very first time?

If you migrate very first time after making new project in Django, you can find that Django creates tables like below.

auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session


Now I learned that those tables are created because lines under INSTALLED_APPS in settings.py.

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]


so I started to look into models.py under auth folder(in Django folder where I installed). I expected that there would be six classes in models.py. Because I learned that Class turn to table thanks to ORM(Ojbejct Relation Mapping).

auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions


I could find class named 'Permission', a class named 'Group' and a class named 'User' in models.py under auth folder. I think those made tables 'auth_permission', 'auth_group' and 'auth_user'. Then what about other?(auth_group_permissions, auth_user_groups, auth_user_user_permissions) I would like to understand how those tables are created by Django(models.py in auth folder). Where should like look into in models.py to understand that?

I expect that I can understand how other tables are created(django_admin_log, django_content_type, django_migrations, django_session) if I can understand that. I will appreciate if you can also explain how Django creates tables named 'django_migrations' and 'django_session' too.

Thank you in advance. Have a nice day.
enter image tables showing in pgAdmin3

Answer

This can be easily explained.

auth_user_groups
auth_group_permissions
auth_user_user_permissions

Are relational tables. Which are used to store info about how model tables are related.

For example auth_user_groups is storing how users and groups are related. If you do

SELECT * FROM auth_user_groups;
|id|user_id|group_id|
....

And here we can see that which groups are related to which users.

So basically django will automatically create such tables for when you use ManyToManyField on your models

Answering comment Django migration table is created automatically when you call ./manage.py migrate for the first time. This table stores history of applying your migrations. Migration model can be found in django/db/migrations/recorder.py. This model is used when running new migrations to see which are applied and which should be applied on this command run. And this model is part of core django's functionality, that's why you don't need to add it in INSTALLED_APPS because INSTALLED_APPS contain only pluggable apps. Which you can include/exclude from your project if you want.

Comments