32👍
You should never just delete migrations before unapplying them, or it will be a nightmare when you want to apply new migrations.
To unapply migrations you should do the following:
-
Use the
python manage.py migrate your_app_name XXXX
in case you want to unapply migrations after the XXXX migration. Otherwise usepython manage.py migrate your_app_name zero
to completely unapply all migrations. -
Remove the
.pyc
files under /migrations/_pycache_/ that you have unapplied. -
Remove the
.py
files under migrations/ that you have unapplied.
Now you can create new migrations without any headaches.
If what you’re looking for is to squash all the migrations into one, do the steps above removing all migrations and then run python manage.py makemigrations your_app_name
to create a single migration file. After that just run python manage.py migrate your_app_name
and you’re done.
7👍
That depends. If you have a production database (or any database you cannot simply drop and recreate), then the answer is no, you cannot safely remove migrations.
If you do not have any permanent databases, then yes, you can remove all migrations, run python manage.py makemigrations --initial
and it will create fresh migrations based on your current models.
Also, you should check if any of the migrations are custom data migrations written by hand. If there are any, you might want to keep those.
The .pyc files are generally safe to remove, provided the related .py files are still there.
your first screenshot is not Django and looks like a JS project of some sort.
- [Django]-How do you dynamically hide form fields in Django?
- [Django]-What does 'many = True' do in Django Rest FrameWork?
- [Django]-Django REST framework post array of objects
2👍
- The json and js files are unrelated to the django migrations as well as __pycache__ folder. You can delete all off them.
- If you mean “previously applied and no longer needed as the project only needs the latest version of the migrations” you don’t want to remove but
squash
them instead withsquashmigrations
which reduces the files you have to two, init file and the initial migration file, this way your project still works. - If by remove you mean you no longer need them because you already changed the models so much that the previous migrations aren’t even used other than being applied and unapplied without ever being used, doesn’t matter, go to step 2 and do that instead of deleting the files manually. When you create migrations on your applications one by one, you also create migration dependency tree, well, django does. And it is really hard to keep track of after some point, if you try to delete everything thinking you can create new migration files with ease, trust me as someone who experienced otherwise, it does not work like that. It is way simpler to let django handle the migration squashing, it optimizes the migration meaning that it also deletes the unused ones in your final state.
More to read at: https://docs.djangoproject.com/en/2.2/topics/migrations/#migration-squashing
- [Django]-Django: how save bytes object to models.FileField?
- [Django]-Django: Fat models and skinny controllers?
- [Django]-Django 1.3.1 compilemessages. Error: sh: msgfmt: command not found
0👍
Having marked one of the answers provided previously as being accepted, here is a summary of a few things I learned:
- Deleting Django migrations is generally a bad idea.
- Django keeps track of what’s in your db through these migration files, as well as through a table it creates in your db, and if you delete any of this Django will start throwing errors on
migrate
that can be hard to fix.
I was getting some of those hard-to-fix errors. Here is what I did to fix it:
- Ran
migrate
on the production server. - When I got an error, it would tell me how the db was out of sync with what Django expected. I corrected that manually by directly editing the db with an sql client.
- E.g. If it said a key existed that wasn’t supposed to exist, I deleted the relevant index from the indicated table.
- Or if it said a table existed that wasn’t supposed to exist, I backed up the table to a file, and deleted the table.
Migrate
then created the table, and then I repopulated it with data from the backup. - In the case of many-to-many tables, once Django had re-created them, I deleted all the new Django-created tables, and restored them from a backup created on my local dev system, which had already had all the latest migrations run on it.
Eventually I was able to complete all migrations successfully.
I have a feeling I lucked out and the above won’t work in all cases! I’ve learned a lot about Django and migrations and will be much more careful about this in the future.
- [Django]-TypeError: data.forEach is not a function
- [Django]-Django-allauth: Linking multiple social accounts to a single user
- [Django]-Django, Models & Forms: replace "This field is required" message
0👍
when you import from third app:
there are 2 step uninstall it
there are use the ‘django_celery_beat’ app for example.
step1: clean table
python .\manage.py migrate django_celery_beat zero
step2: remove app from INSTALLED_APPS
there are done!!!
this is django document on this.
- [Django]-Django: how to do calculation inside the template html page?
- [Django]-Adding css class to field on validation error in django
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
-1👍
How to Reset Migrations
if you are using linux/unix os then you can fire this command. delete all migration directory.
find . -path "/migrations/.py" -not -name "init.py" -delete
find . -path "/migrations/.pyc" -delete
- [Django]-How do I remove Label text in Django generated form?
- [Django]-How does django handle multiple memcached servers?
- [Django]-ForeignKey to abstract class (generic relations)