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
Related
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.
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.
I am working on a Django project with another developer. I had initially created a model which I had migrated and was synced correctly with a MySQL database.
The other developer had later pulled the code I had written so far from the repository and added some additional fields to my model.
When I pulled through his changes to my local machine the model had his changes, and additionly a second migration file had been pulled.
So I then executed the migration commands:
python manage.py makemigrations myapp, then python manage.py migrate in order to update my database schema. The response was that no changes had been made.
I tried removing the migration folder in my app and running the commands again. A new migrations folder had been generated and again my database schema had not been updated.
Is there something I am missing here? I thought that any changes to model can simply be migrated to alter the database schema.
Any help would be greatly appreciated. (Using Django version 1.9).
After you pull, do not delete the migrations file or folder. Simply just do python manage.py migrate. Even after this there is no change in database schema then open the migrations file which came through the git pull and remove the migration code of the model whose table is not being created in the database. Then do makemigrations and migrate. I had this same problem. This worked for me.
In your ProcFile add this line at the beginning.
release: python manage.py migrate
This is such a noob question, but I have an issue with my test database.
I asked the question about a database error I have here.
I now think that if I drop the database and then re-create it, the issue that I have will be resolved.
I am running python 2.7, django 1.7 & postgressql 9.4.
How do I 'delete' the database and then recreate it?
I am unsure if I should drop the entire database from the pgAdmin console and then run migrations. I am unsure because if I do this and it it incorrect, I am not sure what to do next to recover.
I have done a data dump. I have all my migrations in place.
Well, Django by itself does not include what you want.
There are two commands you can use:
python manage.py help flush
This one will destroy all of the data in your database, but will not delete it.
To delete your database you need to run:
python manage.py sqlclear <app>
This command will print to the standard output the sql needed to drop the tables on an specific app, but it will not execute it.
If you really want to delete the database and create it again you can use django_extensions's reset_db
command, which will destroy your database and create it again.
To install django_extensions (assuming you installed pip), just run:
pip install django_extensions
Then add django_extensions to your INSTALLED_APPS variable in settings.py.
After that feel free to run:
python manage.py reset_db
Hope it helps and create fixtures to provide initial data for your models...
I switched from MySQL to PostgreSQL and running migrations and it is a bit strange after MySQL. When I run ./manage.py migrate on a clean db, whenever the migration comes to a field which is ForeignKey or any other relation field, which is not yet created in db, it raises an error and stops. In MySQL you just run migrate and it does all for you, MySQL created these non-existing fields.
So can I somehow to control the execution of migrations like, please postgres go and do that migration first and that second and so on, because otherwise all you gotta do is do migrate manually one by one.
You can explicitly set dependencies between migrations, as described in the docs.