Django, How does models.py under auth folder create initial tables when you migrate very first time?
<p>If you migrate very first time after making new project in Django, you can find that Django creates tables like below.</p>

<pre><code>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
</code></pre>

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

<pre><code>INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
</code></pre>

<p>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).</p>

<pre><code>auth_group
auth_group_permissions 
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
</code></pre>

<p>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?</p>

<p>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.</p>

<p>Thank you in advance. Have a nice day.
<a href="http://i.stack.imgur.com/ExTVQ.png" rel="nofollow">enter image tables showing in pgAdmin3</a></p>

<p>This can be easily explained.</p>

<pre><code>auth_user_groups
auth_group_permissions
auth_user_user_permissions
</code></pre>

<p>Are relational tables. Which are used to store info about how model tables are related.</p>

<p>For example <code>auth_user_groups</code> is storing how users and groups are related.
If you do</p>

<pre><code>SELECT * FROM auth_user_groups;
|id|user_id|group_id|
....
</code></pre>

<p>And here we can see that which groups are related to which users.</p>

<p>So basically django will automatically create such tables for when you use <code>ManyToManyField</code> on your models</p>

<p><strong>Answering comment</strong>
Django migration table is created automatically when you call <code>./manage.py migrate</code> for the first time. This table stores history of applying your migrations. Migration model can be found in <code>django/db/migrations/recorder.py</code>. 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 <code>INSTALLED_APPS</code> because <code>INSTALLED_APPS</code> contain only pluggable apps. Which you can include/exclude from your project if you want.</p>

python django django-models migration