Unable to apply migrations to Heroku postgres database - python

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.

Related

Migrate command not transforming not creating table in database. Django

I created a model and migrated the changes to database and inserted some values in it. But at one point I accidentally deleted that table from database using pgAdmin 4 not from django I was trying to empty the table but accidentally deleted it. The model is still in django and I decided to migrate changes again so that that I could my table back in database. After running migrate it says that no migrations to apply but when I look in the database the table is not there.
I tried creating different models in the same app and tried migrating them they migrated but the model with the name same as model that I deleted is not migrating to the database.
I have deleted all the migrations and it still not taking it.
I think it is still somewhere in the django system from where it should be deleted or something.
Any help how can I get that same name model migrated to the database.
Follow 4 below steps:
Delete the rows related to your app from django_migrations table in your database:
DELETE FROM django_migrations WHERE app='<app_name>';
Go to your app folder and delete migration folder.
Make migration:
py manage.py makemigrations <app_name>
And then migrate:
py manage.py migrate <app_name>

Django migrations not applying after moving from test PC to production server/DB

I am unable to run the inital migrations in my django project on my production server.
I started working on my Django app on my computer with a sqlite db. I was able to run the initial migrations (auth, admin, etc.) and they created tables in my sqlite db with no problem. I was also able to get tables created for the models in my app.
Today I moved the same django project to my webfaction server via git and I am unable to get my MySQL db populated with tables at all. As soon as I created the db, I ran migrate but it said there were no migrations to apply. I tried deleting the migrations folder in my app, but that didn't help at all, probably because there is no django_migrations table to compare any migrations against.
It seems as if the initial migrations are stored where django is installed. This is not part of my git repo, so anything there that might indicate whether the migrations have been made wouldn't be pulled over.
I tried running python manage.py migrate admin
And the same thing with auth, but it did't work
I'm not really sure what the proper way is to go about getting these initial migrations to run again so that I have the proper auth and admin tables in my db. Any help would be appreciated.
This is Django 2.1.7 and Python 3.5
Thanks, Yongjin and Nagesh. By asking me to post my DB settings, you made me realize that I hadn't changed the name of the DB from the project I copied the settings from. Much appreciated!

Django-Cms and Django-shop migration issues

I started creating my website on my own PC using the Django-cms install script then I tried to add a lot of applications such as Django-Helpdesk and others and everything was smooth until I tried to add pieces of djangoSHOP demo project and dependencies to email_auth.
Then I started to have report that the migration history was inconsistent and migrations applied out of order.
Before adding djangoShop to the installed apps, I just add the following commands to setup the DB:
manage.py migrate --run-syncdb --noinput
manage.py migrate --noinput
and everything was ok.
Now, as I'm just starting, I don't have changes to the schema to apply but most of the django apps I try to install have migrations folders with several migrations.
I don't want to update an existing schema, I want to create a new one.
I'm working from a blank database so do I really need to apply all those migrations ? To me it seems that the default behaviour of running migrations is not compatible with starting from a blank db,
I don't understand what to do just initialise the database to just have the required tables created.
Did anyone got the same kind of problems?
How did you fixed it?
Edit:
django-cms>=3.5,<3.6
Django 1.11.19
Error message:
Django InconsistentMigrationHistory: Migration Helpdesk.000xxxxx.py is applied before its dependency email_auth.0001_initial.py on database 'default'

How to restore Django project with pg_dump file?

What is the procedure to restore a Django project using an already restored database from a PostgreSQL pg_dump. All django source code also exist. Will Django migration safe?
If your dump has create table statements and contains all django tables, you can restore it directly onto an empty database. Django will know the status of the migrations as they are stored in a table in the DB.
So the steps would be:
Drop and recreate DB.
If you now run python manage.py showmigrations all migrations will appear unapplied
Restore DB from dump
If you now run python manage.py showmigrations now, the corresponding migrations will appear applied. If your django project has new migrations that weren't applied when the dump was created they will appear unapplied.
And that's it! Now you can apply the new migrations if there are any and keep working on the Django project.

Django model changes cannot be migrated after a Git pull

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

Categories