Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an app from a django projects (and all its tables)

I want to remove an app from a django project.

I want to remove

  • the tables of the app
  • the content-types
  • foreign-key usages of these content-types

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

like image 668
guettli Avatar asked Mar 02 '16 10:03

guettli


People also ask

How do I remove an app from Django project?

To remove the app from your project, all you need to do is remove it from INSTALLED_APPS in your project's settings.py . Django will no longer load the app. If you no longer want the app's files hanging around, delete the app directory from your project directory or other location on your PYTHONPATH where it resides.

How do I remove all objects from a Django model?

How do I delete all records in Django? If you want to remove all the data from all your tables, you might want to try the command python manage.py flush . This will delete all of the data in your tables, but the tables themselves will still exist.

How do I delete a Django project folder?

Assuming you installed via "setup.py install" or equivalent, there is no special method; just delete the directories and any reference to the Django app in e.g. INSTALLED_APPS for any sites you want to keep.

How do I reset Django migrations?

Django's migration can be reset by cleaning all the migration files except __init__.py files under each project app directory, followed by dropping the database and creating migration again using python manage.py makemigrations and python manage.py migrate .


1 Answers

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

Step 1: Delete your table from the file your_app/models.py but leave the file itself

Step 2: Check your register in the file admin.py if you have

Step 3: create migration:

manage.py makemigrations your_app

Step 4: migrate into database:

manage.py migrate

you can see the result in my example enter image description here

Step 5: delete all the files in the folder your_app/migrations

Step 6: Remove migrations in the table django_migrations

python manage.py migrate --fake your_app zero

Check migrations:

python manage.py showmigrations

Step 7: Then you can remove the app completely,check reference,INSTALLED_APPS

like image 92
Lê Vũ Lâm Avatar answered Sep 29 '22 11:09

Lê Vũ Lâm