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.
Related
The database is postgres and framework is django.
Running migrations works fine on localhost but not on Heroku server. I cannot drop or reset the database because it contains important data. I backed up the database, reset it and all migrations worked, but when I restored the backup, it reverted back to the old migration state and the problem persisted.
I don't know how to transfer only the data from the backup into a reset database instead of also transferring the old database structure.
When I run heroku run python manage.py migrate I get this error:
No migrations to apply.
Your models in app(s): 'app1', 'app2', 'app3', 'app4', 'users' have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
Using heroku run python manage.py makemigrations app-name does not help either.
Is there a way to map and transfer just the user data from the old database into the new one? Or is there a way to brute force the old database to accept the new migration structure?
I would be glad if I get some help. Thanks.
I am building a Django web application and I dropped the table django_migrations using psql command line and I want to recreate it.
To recreate django_migrations simply use
./manage.py migrate --fake
But please be careful with that as mentioned here:
If you break something, nobody can help you probably, because the
migration system will not know the current state of the database more.
Therefore do a backup, write notes, use a sandbox and work precisely.
Django migrate --fake and --fake-initial explained
I had my migrations file in version control, and it went out of sync between production and development (I know, that was the mistake, but it's happened now).
As a result, I couldn't run migrations in production. I deleted the migrations folder, and then re-ran makemigrations and migrate. makemigrations logs out creating all the fields. However, migrate simply says "no migrations to apply", and the extra fields do not appear in the database.
All I've changed is adding nullable fields to a model, so it should be a straightforward migration.
I can drop the whole db and start over, but I'd prefer not to because it takes a long time to re-populate.
Is there a way that I can force Django to find the differences between the DB and the models, and build the correct migrations to add the fields?
I attempted adding nonsense models to try and trigger a refresh. But that hasn't changed anything.
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
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.