Reputation: 27189
I want to remove an app from a django project.
I want to remove
Running manage.py migrate app_to_remove zero
does not work:
django.db.migrations.migration.IrreversibleError:
Operation <RunPython <function forwards_func at 0x7ff76075d668>> in
fooapp.0007_add_bar is not reversible
I guess there are several migrations which are not reversible ...
Upvotes: 13
Views: 14962
Reputation: 8761
This is what the official documentation suggests for the latest version as of now, which is 4.2:
- Remove all references to the app (imports, foreign keys etc.).
- Remove all models from the corresponding
models.py
file.- Create relevant migrations by running
makemigrations
. This step generates a migration that deletes tables for the removed models, and any other required migration for updating relationships connected to those models.- Squash out references to the app in other apps’ migrations.
- Apply migrations locally, runs tests, and verify the correctness of your project.
- Deploy/release your updated Django project.
- Remove the app from
INSTALLED_APPS
.- Finally, remove the app’s directory.
Upvotes: 2
Reputation: 27189
app_to_remove
from settings.INSTALLED_APPS
urls.py
or other placesCreate an empty migration for your django-project:
manage.py makemigrations your_django_project --empty
Edit the file. Here is a template:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('your_django_project', '0001_initial'),
]
operations = [
migrations.RunSQL('''
drop table if exists app_to_remove_table1;
drop table if exists app_to_remove_table2;
....
delete from auth_permission where content_type_id in (select id from django_content_type where app_label = '{app_label}');
delete from django_admin_log where content_type_id in (select id from django_content_type where app_label = '{app_label}');
delete from django_content_type where app_label = '{app_label}';
delete from django_migrations where app='{app_label}';
'''.format(app_label='app_to_remove'))
]
Run the migration, run tests.
About "drop if exists": You have two cases:
Upvotes: 23
Reputation: 337
Note: this guide is successful with Django 3.1.1 and Python 3.8.2
Can you try this solution to clean your database and migrations first
manage.py makemigrations your_app
manage.py migrate
you can see the result in my example
python manage.py migrate --fake your_app zero
Check migrations:
python manage.py showmigrations
Upvotes: 1