Broken makemigrations in django - django

I added I new app in django project. After that I made a migration and migrate on it. Then I changed some field in model, and after trying to create some instance it was mistake with this field. So after that I made I terrible mistake and delete all migrations in this app, and now I can't make new migrations.
I have such error: "Migration ... dependences reference nonexistent parent node" some where in old app. But this migration is exist.

If you need to delete a migration you should run reverse migrations before deleting a migration. So if you're on migration 0004 and you made a mistake in that and wanted to go back, run the reverse to the previous migration e.g.
python manage.py migrate myapp 0003
This brings the database tables for myapp to the state they were in for migration 0003.
If you wanted to correct something, or re-create an initial migration for a feature you're making and you don't need to keep the test data you've created you should run;
python manage.py migrate myapp zero
This will run the backwards migration on all migrations for myapp including the initial migration.
If you've deleted all your migration files and you now want to do something with migrations for that app you'll have to delete the tables in the database relating to your app, then remove the rows from the migrations table (django_migrations) which relate to any migrations from your app and simply recreate the initial migration again.
The rows in the migrations table looks like this;
1 contenttypes 0001_initial 2015-04-16 10:09:38
So just look for your app name in that table.

Related

Delete Heroku migrations

I read advices how to delete migrations but I don't understand what I'm doing and it's not working for me.
History. One day I had an issue when I added or renamed a model fields locally. So I was tired with that issue and I deleted all migrations and migrate again. And all was OK. But I remember that I will have a big problem when I will deploy on Heroku.
So the days are gone. And now it happened. :(((
I make migrations, migrate to a server database. Pushed my code, but.. it wrote me:
relation "accounts_goal" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "accounts_goal"
I understand it happened because locally I have 0001 and 0002 migrations, but on the server there are 0012 and etc. migrations. I think I need to delete all old migrations on the server. But I don't know how to do that. Help me please! Thank you)
At first, do not delete a table from the database before the migration. If you change your model, then manage.py migrate will do the thing. Django cannot detect the direct change of database; only knows the change of model script (makemigrations).
Sometimes migration doesn't work for no reasons. In that case, do the following things:
Undo the change of models.py (comment, delete).
manage.py makemigrations app-name.
manage.py migrate
Change the models.py again (as you wish).
Do the migration again.
Also you can try python manage.py migrate --fake.
If there is already a table present, Django will see that the initial migration has already been applied since the table is already present with old schema and therefore not taking the new table with different schema into consideration.
In that case, you need to drop tables in the database you can use python manage.py migrate app-name zero.

why django can not create a table after deleting the table and the migration files of the app?

I am working with Django v4.* which I connected it to Postgres DB on the localhost,
I have created my model (Article) then makemigrations then migrate
then I have changed the model by adding extra field, as a result it didn't take effect so I have deleted the table and all the migrations files in articles/migrations folder apart of the __init__.py file, then I did makemigrations then migrate it create a new file 0001_initial.py but its not creating a new table into the DB, unless I drop the whole DB, which is not ideal in the production env!
I am wondering why Django is unable to create the table back again? and how I can get it created as a new table?
Roll back to your initial migration by running below command:
python manage.py migrate --fake <appname> 0001
Followed by:
python manage.py migrate <appname>
Just delete your database with
python manage.py flush
And delete migrations files
You may have mistyped the directory in the terminal

Django migrations for a cloned db

I cloned a postgres db and added a new model to one of the apps. Our project contains many apps.
Now When I ran migrations, migrate it would fail. So I commented that model out, deleted migrations.py file from folder and ran fake migrations. Than again put that model in and ran migrations, migrate. Things were fine.
But now I deleted this model table from db manually and when would run migrations it would show model doesn't exist.
Basically I would need to again and again tweak the model, delete, update table.
So I searched for migrating from scratch. Did delete some apps from django_migrations table. But it is not working out it shows relations already existing.
This is all becoming confusing, --fake, delete, squash what to do?
Basically if I drop table django_migrations, delete migrations folder from app. Can't django automatically sync with db and understand what model exist and what don't and figure it out itself.
If you want to create a clone of a Django database, without the data, there's one table you should always get the data from: django_migrations. This is the table that holds the state of the database: which migrations have been applied and as such what models exist already.
The fact that Django tries to create one it's core models tells me you didn't have the data from django_migrations in the clone.
This solved the problem.
deleted all the data from 'django_migrations' table.
deleted all the migrations file from all migrations folder in different apps
Commented out the models whose table are still not in database
ran python manage.py makemigrations
ran python manage.py migrate --fake
Put the comments off the model
ran python manage.py makemigrations
ran python manage.py migrate
Thing is to understand what does makemigrations and migrate does. And then you can delete/update manually or created new models and migrate/clone things etc. it would work. For a new starter these commands are little confusing.
Let's says everything is empty i.e. no files in migration folder of app.
Than makemigrations makes these files which contains say sqls/syntax on how to create your table. With each change it will create a new one, with only the changes listed in that.
migrate creates actual empty tables in the database. And creates an entry in django_migrations against the app that model was part of. This entry tells it when was the latest entries from that file were applied to the database.
Scenario 1:
I deleted one table manually from db. Now django doesn't know about it. So if you would run makemigrations or migrate it would do nothing.
Scenario 2:
I deleted all entries from django_migrations, deleted migrations files from app. Tables are still in db. Now when I do makemigrations it works, but when I migrate it throws error that already existing.
Scenario 3:
Have deleted entries from django_migrations and migration files. Django has one option of migrate --fake, this tells it that tables are already existing just make entries in django_migrations table. so makemigrations and migrate --fake works now without error. But now the table which I deleted manually is still deleted, django hadn't made it. So will throw error when I try to access it.
Scenario 4:
The one I described at start. I faked the entries which were in db and then migrated the model which wasn't in db.
So once one understands what's it's doing behind the scenes many approaches could have been followed, create the table schema manually or selectively pick django_migrations or migrations file entries and delete them. But best is to delete everything and start from scratch.
It would have been best that django understands that these are tables already created, these are models I have, create the missing one, leave the existing ones. If any discrepancy show them. I see so many questions related to migrations and people are confused here and there.

Deleted migration files- how to makemigrations without losing data

I changed a field on a model class (which has no classes point to it, only one foreign key pointing out of it). Somehow, this stuffed up my migrations and it keeps saying "django.db.migrations.graph.NodeNotFoundError:" looking for migration files that do not exist.
I accidentally deleted several files in my 'migrations' folder.
My database contains a lot of data, and I do not want to break it.
Will I lose any data if I:
Remove the table that caused the problem in the first place (psql, \d, DROP TABLE tablename)
delete all my migration files
Re run the migration from the start?
./manage.py makemigrations
./manage.py migrate
Can anyone recommend another way of fixing this?
Here is the traceback:
http://dpaste.com/0Y1YDXS
Aren't you using git so that you can get your migration files back? If not, install and use it, starting now
I would suggest:
make a backup/dump of your database first, in case something goes wrong
Delete all migrations
Empty migration table in psql
call makemigrations
call migrate --fake-initial
Empty the django_migrations table:
delete from django_migrations;
Remove all the files in migrations folders in each and every app of your project.
Reset the migrations for the "built-in" apps:
python manage.py migrate --fake
Create initial migrations for each and every app:
python manage.py makemigrations your_app_name
Final step is to create fake initial migrations:
python manage.py migrate --fake-initial
See here https://micropyramid.com/blog/how-to-create-initial-django-migrations-for-existing-schema/

Why there is no migrations detected but I just created new migrations?

This is my workflow, everything seems nice and tidy with makemigrations and migrate. However, I want to start over with my models, so I:
rm -rf all the migrations file in the django app directory;
drop all the related tables in the database;
makemigrations results in new migration files;
But when I want to migrate it into database, it is said No migrations to apply. I think the mechanism behind migrations is that models -> migrations -> database changes, if I drop the latter two it should go over again. Unfortunately it is not.
Django == 1.8.2
CLOSED There is a table called django-migrations tracking all the changes and delete related records and then I could make migrations. Thx to those who helped!
There's another table that keeps track of which migrations have been ran for each app, django_migrations. You'll have to reset that in order for django to know you have started over.
if you changed the database already then try running
python manage.py migrate <app> --fake zero