Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the Django migrations folder?

I must be doing something wrong. Everywhere I see people saying "Look at the migrations folder" but even though I can see migrations there is no folder.

Karls-Mac-mini:django_test karl$ tree
├── django_test
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   ├── settings.cpython-35.pyc
│   │   └── urls.cpython-35.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

Karls-Mac-mini:django_test karl$ python manage.py showmigrations
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial
like image 945
Karl Penzhorn Avatar asked Sep 19 '18 09:09

Karl Penzhorn


People also ask

What is migration file in Django?

Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They're designed to be mostly automatic, but you'll need to know when to make migrations, when to run them, and the common problems you might run into.

Can I delete migration folder in Django?

so you need to delete migration from the database as well. if you are using sqllite3(by default database) then delete the . sql file or if you are using PostgreSQL or any other database then delete the migration file from the django_migration database.


2 Answers

Short answer: the migrations originate from Django apps and third party apps you installed in INSTALLED_APPS. Not the ones you defined yourself.

Migrations are generated per app, and are stored in some_app/migrations.

Even if you do not define migrations for your apps, there will usually be migrations that take place, since you (likely) included some apps defined by Django (and other third parties) in your INSTALLED_APPS, these have migrations as well.

For example most likely your INSTALLED_APPS contains:

# settings.py

INSTALLED_APPS = [
    # ...
    'django.contrib.auth',
    # ...
]

If we take a look at the source code of this app [GitHub], we see the migrations directiory. By adding this app to the INSTALLED_APPS you thus have added apps defined in the Django library itself (or by third parties) to your project, and the migrations of these apps are thus processed in the same way (in fact there is nothing "magical" about these apps, it is more that these handle common problems such that you do not need to care about these anymore).

The django.contrib.auth app has a file structure like (ommitting noise):

django/
    contrib/
        auth/
             migrations/
                __init__.py
                0001_initial.py
                0002_alter_permission_name_max_length.py
                0003_alter_user_email_max_length.py
                0004_alter_user_username_opts.py
                0005_alter_user_last_login_null.py
                0006_require_contenttypes_0002.py
                0007_alter_validators_add_error_messages.py
                0008_alter_user_username_max_length.py
                0009_alter_user_last_name_max_length.py

These are extactly the same migrations you see on the console when you perform migrations for the auth app (second section).

like image 197
Willem Van Onsem Avatar answered Sep 25 '22 03:09

Willem Van Onsem


Django Project is actually a combination of a few Applications and Configuration files.

Applications and Configuration are actually Python Modules/Packages. Every project has a few default apps installed and they are mentioned in INSTALLED_APPS (see project settings.py).

These are the default apps and they are not stored/installed in your project: they reside inside Django package by default.

Example:

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

For example, django.contrib.admin app is used with your application: the migrations directory for this app is available at site-packages/django/contrib/admin/migrations, but migrations from apps that you created, are all stored inside each app folder.

like image 40
Sopan Avatar answered Sep 21 '22 03:09

Sopan