Django migrations - django.db.migrations.exceptions.NodeNotFoundError - python

I get the following error when I run any python manage.py function:
raise NodeNotFoundError(self.error_message, self.key, origin=self.origin)
django.db.migrations.exceptions.NodeNotFoundError: Migration auth.0010_user_following dependencies reference nonexistent parent node ('accounts', '0002_auto_20180615_2021')
It happened after I tried to reset my migrations by manually deleting the migration files in the migration folders (except the init files - no other files are left in the migration folders).
I have tried dropping the database with python manage.py flush, which also doesn't run.
Any suggestions? Thanks!
SOLUTION:
After a week of google search I ended up reconstructing the referenced migration files manually using the documentation: https://docs.djangoproject.com/en/2.0/howto/writing-migrations/
After that the manage.py migrate and makemigration functions worked again. Never delete migration files without taking a backup first!

SOLUTION: After a week of google search I ended up reconstructing the referenced migration files manually using the documentation: https://docs.djangoproject.com/en/2.0/howto/writing-migrations/
After that the manage.py migrate and makemigration functions worked again. Never delete migration files without taking a backup first!

Unfortunately, manually deleting migrations doesn't reset them. The database knows which migrations have been run, and the error you're seeing is from Django trying to check whether the state of the models in your models modules matches the state of the migrations that have been run (which is to say, whether or not you need to create migrations to match) and also whether there are migrations that have been created but not run--these cases would create warnings. In trying to check these things, it tries to load migrations and can't find any of them.
If you want to reset your migrations, and just have a single migration per app to go from an empty database to your current schema in a single step, I recommend using the squash migrations command. You'll need to have the migrations files back first, though.
Alternatively, if you do want to drop and re-create the database altogether, you'll need to do that outside of the management commands, since those do the above checks when they run. Then you can have an empty database and run ./manage.py makemigrations and you'll get initial migrations that represent models as they are.

I faced the exact same error, and found the constructing the whole database a little complicated
I found a way to simply delete the whole database and reload it , (if its useful for you) using the following commands:
Step 1:
find . -path "/migrations/.py" -not -name "init.py" -delete
find . -path "/migrations/.pyc" -delete
Step 2:
Delete the database (sqlite3 in my case)
Step 3:
Run
python manage.py makemigrations
python manage.py migrate
They should now run successfully

If you have manually deleted your migrations folder, here are options to directly get them back instead of manually re-creating them. Can't imagine the pain.
Reset that deletion
To get them back you can do a git reset on the deleted folder.
git checkout -- initial/path/to/migrations
This should recover them if they were being tracked. This also assumes changes were not yet commited. If they were commited, see this answer
In case the migrations were .gitignored
If the migrations were not tracked by git, you can result to get a clean working tree where django hadn't indexed the run migrations
Clone the repository anew in a separate directory. Run the migrations python manage.py makemigrations && python manage.py migrate
Copy the deleted migrations to your migration-deleted working tree (So your can continue where you had left off)
The above option will work if the changes were unpushed to the remote. Otherwise, you'll have to checkout a commit made before the deletions.
The provided options assume you were using git

i am just uninstalled my current Django
then re-installed
its worked for me
pip3 uninstall Django
pip3 install Django

Related

Why does Django not see migrations?

I am trying to change some field names of Django models, but for some reasons Django does not want to see my migrations. I made sure the following things are properly set up:
myApp.apps.MyAppConfig is added to INSTALLED_APPS in settings.py
A migration folder with an empty __init__.py is present in my app folder
In myApp folder, I also have an empty __init__.py
The whole procedure works just fine locally with a SQLite database and I can change field names and run python manage.py makemigrations and python manage.py migrate to migrate the database. However in production where I use Docker Compose for orchestration and Postgres as database only the first migration works fine. Afterwards when I change any model field name and try to run docker-compose exec projectname django-admin makemigrations or docker-compose exec projectname python manage.py makemigrations or add the app name behind these commands nothing helps.
Then Postgres can't handle the request, because it doesn't know the new field name:
2022-03-11 14:40:22.136 UTC [81] ERROR: column model_name.new_field_name does not exist at character 357
What am I missing here? How can I get Django to migrate?
I had the same issues when using version control to submit changes to another environment.
What probably happens is that django believes it already made the migration, because of the information that is being passed along when you push those changes. What you need to do is to correct that behavior manually deleting those migrations or, if you don't need to keep information yet, force the migration from the beginning.
You may want to read the docs for future use, and I used the answers in this questions to solve the same issue.
At the end I was able to solve this issue by adding a Docker volume for the migrations folder. I believe a lot of times people put their entire code in a persistent volume, which would've also prevented this issue. However at least everything that needs to be persistent (obviously) needs a persistent volume.

flask-migrate alembic.util.exc.CommandError python

I am using flask-migrate to update the changes in my database. I ran this command. and then this command
$python manage.py db init
$python manage.py db migrate
I get the error below
alembic.util.exc.CommandError: Can't locate revision identified by 'e462fd034cc1'
I looked on stackoverflow for similar problems where it was suggested to deleted the migrations folder which i did but still same error is coming again and again.
What should i do.
Your database is out of sync with your migrations repository. For some reason the latest migration id stored in the database is not the migration id of a migration in your repository. This means that you probably deleted or modified the migration repository by hand and made it inconsistent with the current state of your database.
If this is a scratch database, maybe deleting and running the migrations again will fix the problem and give you a valid database.
Just see what is your last migration number.
Open migrations/versions folder in any file manager and sort by date.
For me, for example, it e222b725dce9_.py
Then change the value in version_num column in alembic_version table
Note that I delete underscrore in the end
Then run python manage.py db migrate and python manage.py db upgrade
All must pass successfully

How do I find the latest migration created w/ flask-migrate?

My flask application now has 20+ migrations built with flask-migrate and they all have hashed file names like: 389d9662fec7_.py
I want to double check the settings on the latest migration that I ran, but don't want to open every file to look for the correct one. I could create a new dummy migration and look at what it references as the down_revision but that seems clunky.
I'm using flask-script, flask-migrate, and flask-sqlalchemy
My question is: How can I quickly find the latest migration that I created?
./manage.py db history -r current: will show the migrations in the order they will be applied. -r current: shows only the migrations since the currently applied one.
./manage.py db heads will show the most recent migration for each branch (typically there's only one branch). ./manage.py db upgrade would apply all migrations to get to the head.
Use the -v flag to get verbose output, including the full path to the migration.
You can also check in your database and the current version should be displayed in a table called alembic_version.

Stuck in a django migration IntegrityError loop: can I delete those migrations that aren't yet in the db?

So, I committed and pushed all my code, and then deployed my web application successfully. Then, I added a new model to my 'home' app, which (for a reason I now understand, but doesn't matter here), created an IntegrityError (django.db.utils.IntegrityError: insert or update on table "foo" violates foreign key constraint "bar"). I ran python manage.py makemigrations, python manage.py migrate, which causes the the IntegrityError.
However, even if I remove all of my new model code(so that git status comes up with nothing), the IntegrityError still happens. If I connect to my db via a different python instance and download select * from django_migrations;, the latest db migration: 0020 there is eight migrations away from my latest local home/migrations migration file: 0028.
--> My question is: is it safe for me to delete my local 0021-0028 migration files? Will this fix my problem?
If you haven't applied your migrations to db, it is safe to delete them and recreate them.
Possible reasons of why you run into this error are:
You deleted your model code but, when you run migrate it reads your migration files (which has information about your deleted model) and tries to apply migration operations. If you didn't run makemigrations command after you've deleted your model, migration system won't be able to detect your changes and will think that your model is still there.
Even if you've run makemigrations after you've deleted your model there'll be dependency issues in your migrations files, because the new migration files will depend on old ones (with which you had problems)
That's why we can say that it is safe to delete them, if they haven't applied, but at the same time you should be careful with your migration dependencies.
This documentation information maybe useful.
OK, so I crossed my fingers, backed my local 0021-0028 migration files, and then deleted them. It worked. I think they key is that the migration files were not yet in the database yet, but not 100% sure. +1 if anyone can answer further for clarification.

Django 1.7 - makemigrations not detecting changes

As the title says, I can't seem to get migrations working.
The app was originally under 1.6, so I understand that migrations won't be there initially, and indeed if I run python manage.py migrate I get:
Operations to perform:
Synchronize unmigrated apps: myapp
Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
No migrations to apply.
If I make a change to any models in myapp, it still says unmigrated, as expected.
But if I run python manage.py makemigrations myapp I get:
No changes detected in app 'myapp'
Doesn't seem to matter what or how I run the command, it's never detecting the app as having changes, nor is it adding any migration files to the app.
Is there any way to force an app onto migrations and essentially say "This is my base to work with" or anything? Or am I missing something?
My database is a PostgreSQL one if that helps at all.
If you're changing over from an existing app you made in django 1.6, then you need to do one pre-step (as I found out) listed in the documentation:
python manage.py makemigrations your_app_label
The documentation does not make it obvious that you need to add the app label to the command, as the first thing it tells you to do is python manage.py makemigrations which will fail. The initial migration is done when you create your app in version 1.7, but if you came from 1.6 it wouldn't have been carried out. See the 'Adding migration to apps' in the documentation for more details.
This may happen due to the following reasons:
You did not add the app in INSTALLED_APPS list in settings.py
(You have to add either the app name or the dotted path to the subclass of AppConfig in apps.py in the app folder, depending on the version of django you are using). Refer documentation: INSTALLED_APPS
You don't have migrations folder inside those apps. (Solution: just create that folder).
You don't have __init__.py file inside migrations folder of those apps. (Solution: Just create an empty file with name __init__.py)
You don't have an __init__.py file inside the app folder. (Solution: Just create an empty file with name __init__.py)
You don't have a models.py file in the app
Your Python class (supposed to be a model) in models.py doesn't inherit django.db.models.Model
You have some semantic mistake in definition of models in models.py
Note:
A common mistake is to add migrations folder in .gitignore file. When cloned from remote repo, migrations folder and/or __init__.py files will be missing in local repo. This causes problem.
Migration files are supposed to be included in the repo. read here. If your team frequently faces migration issues you may consider ignoring migration files as follows:
I suggest to gitignore migration files by adding the following lines to .gitignore file
*/migrations/*
!*/migrations/__init__.py
Remember, it is not recommended to gitignore migration files as per django documentation
Ok, looks like I missed an obvious step, but posting this in case anyone else does the same.
When upgrading to 1.7, my models became unmanaged (managed = False) - I had them as True before but seems it got reverted.
Removing that line (To default to True) and then running makemigrations immediately made a migration module and now it's working. makemigrations will not work on unmanaged tables (Which is obvious in hindsight)
My solution was not covered here so I'm posting it. I had been using syncdb for a project–just to get it up and running. Then when I tried to start using Django migrations, it faked them at first then would say it was 'OK' but nothing was happening to the database.
My solution was to just delete all the migration files for my app, as well as the database records for the app migrations in the django_migrations table.
Then I just did an initial migration with:
./manage.py makemigrations my_app
followed by:
./manage.py migrate my_app
Now I can make migrations without a problem.
Agree with #furins. If everything seems to be in order and yet this problem arises, checkout if there is any property method with same title as the attribute which you are trying to add in the Model class.
Remove method with similar name as attribute you are adding.
manage.py makemigrations my_app
manage.py migrate my_app
Add the methods back.
This is kind of a stupid mistake to make, but having an extra comma at the end of the field declaration line in the model class, makes the line have no effect.
It happens when you copy paste the def. from the migration, which itself is defined as an array.
Though maybe this would help someone :-)
Maybe I am too late but did you try to have a migrations folder in your app with a __init__.py file in it?
Maybe this will help someone. I was using a nested app. project.appname and I actually had project and project.appname in INSTALLED_APPS. Removing project from INSTALLED_APPS allowed the changes to be detected.
The answer is on this stackoverflow post, by cdvv7788 Migrations in Django 1.7
If it is the first time you are migrating that app you have to use:
manage.py makemigrations myappname Once you do that you can do:
manage.py migrate If you had your app in database, modified its model
and its not updating the changes on makemigrations you probably havent
migrated it yet. Change your model back to its original form, run the
first command (with the app name) and migrate...it will fake it. Once
you do that put back the changes on your model, run makemigrations and
migrate again and it should work.
I was having the exact same trouble and doing the above worked perfectly.
I had moved my django app to cloud9 and for some reason I never caught the initial migration.
Following worked for me:
Add the app name to settings.py
use 'python manage.py makemigrations'
use 'python manage.py migrate'
Worked for me: Python 3.4, Django 1.10
People like me who don't like migrations can use steps below.
Remove changes what you want to sync.
Run python manage.py makemigrations app_label for the initial migration.
Run python manage.py migrate for creating tables before you make changes.
Paste changes which you remove at first step.
Run 2. and 3. steps.
If you confused any of these steps, read the migration files. Change them to correct your schema or remove unwanted files but don't forget to change next migration file's dependencies part ;)
I hope this will help someone in future.
You want to check the settings.py in the INSTALLED_APPS list and make sure all the apps with models are listed in there.
Running makemigrations in the project folder means it will look to update all the tables related to all the apps included in settings.py for the project. Once you include it, makemigrations will automatically include the app (this saves a lot of work so you don't have to run makemigrations app_name for every app in your project/site).
Just in case you have a specific field that does not get identified by makemigrations: check twice if you have a property with the same name.
example:
field = django.db.models.CharField(max_length=10, default = '', blank=True, null=True)
# ... later
#property
def field(self):
pass
the property will "overwrite" the field definition so changes will not get identified by makemigrations
Adding this answer because only this method helped me.
I deleted the migrations folder run makemigrations and migrate.
It still said: No migrations to apply.
I went to migrate folder and opened the last created file,
comment the migration I wanted(It was detected and entered there)
and run migrate again.
This basically editing the migrations file manually.
Do this only if you understand the file content.
Make sure your model is not abstract. I actually made that mistake and it took a while, so I thought I'd post it.
Did u use schemamigration my_app --initial after renaming old migration folder? Try it. Might work. If not - try to recreate the database and make syncdb+migrate. It worked for me...
In my case I needed to add my model to the _init_.py file of the models folder where my model was defined:
from myapp.models.mymodel import MyModel
I had mistakely deleted folder of migrations from my project directory.
Solution is to create __init__.py file in the migrations folder, and then,
python manage.py makemigrations
python manage.py migrate
Had the same problem
Make sure whatever classes you have defined in models.py, you must have to inherit models.Model class.
class Product(models.Model):
title = models.TextField()
description = models.TextField()
price = models.TextField()
I had the same problem with having to run makemigrations twice and all sorts of strange behaviour. It turned out the root of the problem was that I was using a function to set default dates in my models so migrations was detecting a change every time I ran makemigrations. The answer to this question put me on the right track: Avoid makemigrations to re-create date field
I recently upgraded Django from 1.6 to 1.8 and had few apps and migrations for them. I used south and schemamigrations for creating migrations in Django 1.6, which is dropped in Django 1.8.
When I added new models after upgrade, the makemigrations command wasn't detecting any changes. And then I tried the solution suggested by #drojf (1st answer), it worked fine, but failed to apply fake initial migration (python manage.py --fake-initial). I was doing this as my tables (old tables) were already created.
Finally this worked for me, removed new models (or model changes) from models.py and then had to delete (or rename for safety backup) migrations folder of all apps and run python manage.py makemigrations for all apps, then did python manage.py migrate --fake-initial. This worked like a charm. Once initial migration is created for all apps and fake initial migrated, then added new models and followed regular process of makemigrations and migrate on that app. The changes were detected now and everything went fine.
I just thought of sharing it here, if someone faces same problem (having schemamigrations of south for their apps), it might help them :)
Maybe that can help someone, I had the same problem.
I've already created two tables with the serializer class and the views.
So when I wanted to updated, I had this error.
I followed this steps:
I made .\manage.py makemigrations app
I executed .\manage.py migrate
I erased both tables of my models.py
I erased all reference to my tables from serializer and view class.
I executed step 1 and 2.
I retrieved my changes just in the models.py
I executed again step 5.
I restored all my changes.
If you're working with Pycharm, local history is very helpfull.
Maybe this will help someone.
I've deleted my models.py and expected makemigrations to create DeleteModel statements.
Remember to delete *.pyc files!
./manage makemigrations
./manage migrate
Migrations track changes to DB so if youre changing from unmanaged to managed, you'll need to make sure that youre database table is up to date relating to the Model you're dealing with.
If you are still in dev mode, I personally decided to delete the migration files in my IDE as well as in the django_migrations table relating to my Model and rerun the above command.
REMEMBER: if you have a migration that ends with _001 in your IDE & _003 in your database. Django will only see if you have a migration ending with _004 for anything to update.
The 2 (code & db migrations) are linked and work in tandem.
Happy coding.
You may need to fake the initial migrations using the command below
python manage.py migrate --fake-initial
Remove changes what you want to sync.
Run python manage.py makemigrations app_label for the initial migration.
Run python manage.py migrate for creating tables before you make changes.
Paste changes which you remove at first step.
Run 2. and 3. steps
Added this answer because none of other available above worked for me.
In my case something even more weird was happening (Django 1.7 Version), In my models.py I had an "extra" line at the end of my file (it was a blank line) and when I executed the python manage.py makemigrations command the result was: "no changes detected".
To fix this I deleted this "blank line" that was at the end of my models.py file and I did run the command again, everything was fixed and all the changes made to models.py were detected!
First this solution is applicable to those who are facing the same issue during deployment on heroku server, I was facing same issue.
To deploy, there is a mandatory step which is to add django_heroku.settings(locals()) in settings.py file.
Changes:
When I changed the above line to django_heroku.settings(locals(), databases=False), it worked flawlessly.
I have encountered this issue, the command
python manage.py makemigrations
worked with me once I saved the changes that I made on the files.
One of the cause may be You didn't register your models in admin.py file .
First register your models in admin.py file then do the migrations.

Categories