Django: "No migrations to apply" for existing server database - django

Added a small model 'new_model' in existing app 'existing_app' on Django,
Entered commands manage.py makemigrations and manage.py migrate. It migrated perfectly the changes to local database.
Tried to apply same on heroku but its not working there. I then tried to setup heroku database in local settings.py of project and migrations worked, but it shown some well known error. I have attached the output with this discusssion. Attaching the final screenshot the final screenshot as output.
Just In Case if it may help_________
The following are the methods I have applied before the final solution:-
With heroku run manage.py makemigrations
Adding makemigrations command to procfile
Pushing migrations as well, so it may replace server migrations
Heroku bash and tried "makemigration" and "migrate" on it
Alternate solution result Alternate solution result
The all four mentioned steps provided this output:- "No migrations to apply, and on migrate it shows you dont have new migrations",
helping out to migrate changes on database.
Thank You.

Related

Django migrate fails when creating new table

We are fairly new to Django. We we have an app and a model. We'd like to add an 'Category' object to our model. We did that, and then ran 'python manage.py makemigrations'.
We then deploy our code to a server running the older code, and run 'python manage.py migrate'. This throws 2 pages of exceptions, finishing with 'django.db.utils.ProgrammingError: (1146, "Table 'reporting.contact_category' doesn't exist")'
This seems to be looking at our models.py. If we comment out Category from our model, and all references to it, the migration succeeds.
I thought that the point of migrations is to make the database match what the model expects, but this seems to require that the model match the database before the migration.
We clearly are doing something wrong, but what?
I believe you skipped some migration in the server, so now you are missing some tables (I have been in that situation. Ensure migrations directories are on your .gitignore. You CAN NOT check in migrations files, you have to run makemigrations on the server). This can be solved by tracing back up to the point the database and models files match, but it is a risky process if it is your production database, so you should make a full backup before proceeding, and try the process on a different computer before.
This would be my advice:
Delete migration files from the server.
Comment the models that rise the error.
Set the server's migration history to the point the
database is, using python manage.py makemigrations and python manage.py migrate --fake-initial (this will update the migration files without actually attempting to modify the database).
Uncomment the models that raise the error.
Run python manage.py makemigrations and python manage.py migrate.
If, after you comment the models that raise the exception, you get a different exception, you have to keep on commenting and attempting again. Once a migrations succeeds, you can uncomment all commented models and make an actual migration.
Remember to run python manage.py makemigrations if you made changes to the models.py then run python manage.py makemigrations
Both commands must be run on the same server with the same database

Problem running migrations in Production (i.e. Heroku platform)

I made some changes to the db (i.e. added on column). I can make and run migrations just fine in my local env, the problem is in Production. I have made the migrations and pushed them to my remote repo, but when I run the migrations in production, nothing happens. I can makemigrations in production but when I run "heroku run python manage.py migrate", the system says, that are no migrations to implement.
This is worth mentioning though, initially I ran "heroku run python manage.py migrate --fake" because I had a lot intermediary tables that threw the error "table xxxxxx already exist" everytime I tried to run a migrations, so that fixed that problem but I believe that the actual change that I wanted(i.e. adding a column to the db) to make, actually got migrated as fake migration. How can I fix this?

relation "django_admin_log" already exists

when i try to run python manage.py migrate i run into following error
Upon running python manage.py run migrations it says no changes detected. and when i runserver it gives me warning that i have unapplied migrations as well.i have been searching internet for two hours but got not solution. Someone knowing the solution please share :)
The table in your database that stores migration data to keep track of what has been applied is out of date. Try running python manage.py migrate --fake
Try python manage.py makemigrations [app name] and if still, this does detect changes then delete the folder named migrations which is inside your application folder and then use this python manage.py makemigrations [app name]. Once migration happens successfully do the python manage.py migrate.
Don't Try This at Home
I faced this issue, i make two changes,
change AUTH_USER_MODEL, so i have one migraiton about it
second one add new field for my folder_model(migration name: folder_model 0021)
When my first migrate attempt(I already run makemigrations commands on local so i have migration files), it says;
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'
This error reiases because i change the AUTH_USER_MODEL in the middle of the project, normally you have to remove your database and fresh start from the beginnig(also truncate migrations etc.), according to Django doc -> https://code.djangoproject.com/ticket/25313
To fix this issue, you don't have to delete all migrations on db, just delete the migrations about admin(not from project just database)
After that just run
python manage.py migrate
It throws relation "django_admin_log" already exists. For this issue, run:
python manage.py migrate --fake
That's it, but not completely. Make fake migration act like you already make your all migrations successfully and save these on db. The issue came here that i have another migration about folder_model 0021 and with fake migration it doesn't applied to my database table but saved to db_migrations table.
So fix this issue, delete the folder_model 0021to database migration table (just 0021 not all folder_model migrations).
After delete just run python manage.py migrate
Everything is fine!

django migration No migrations to apply

I have screwed up my database so I tried to redo it. I did:
1) deleted all tables via sql
2) deleted the migrations folder
So when I now do manage.py makemigrations myapp it is creating the migration folder and the initial.py file, which looks fine. Also the __init__.py is there. However, if i do manage.py makemigrations myapp I always get the message "No migrations to apply."
I looked up the database in the shell and the tables are not there.
I am using Django 1.8.
Django keeps track of all the applied migrations in django_migrations table.
So just delete all the rows in the django_migrations table that are related to you app like:
DELETE FROM django_migrations WHERE app='your-app-name';
and then do:
python manage.py makemigrations
python manage.py migrate
Django keeps track of all the applied migrations in django_migrations table.
So, you can follow this method:
Delete the related rows from django_migrations.
run python manage.py migrate.
I usually ran into that issue myself. Here is the workaround I found:
Replace your database sqlite3 with this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_username>$<your_database_name>',
'USER': '<your_username>',
'PASSWORD': '<your_mysql_password>',
'HOST': '<your_mysql_hostname>',
}
}`
The fill in for can be found in the databases tab on your Pythonanywhere dashboard.
Push it to github from your Terminal and pull it down from Pythonanywhere Bash again. You may have to add this on Bash Console: pip install mysqlclient
As #Anush mentioned, Django does keep track of all of the migrations in the django_migrations table. As mentioned, you can use raw SQL to remove the migration row to reset Django's migration history for the concerned app.
DELETE FROM django_migrations WHERE app='my-app';
If you are uncomfortable deleting rows like this, then you can use Django's management command replacing my-app for the name of your app.
python manage.py migrate --fake my-app zero
That command will remove the migration history rows from the database leaving you to start your migration again from scratch. You will then need to:
Delete all of the migration files within your app's 'migrations' directory but ensure that the __init__.py file remains.
Ensure that your app's data models are set up as you need them
Make the migrations again with the command python manage.py makemigrations {your_app_name}. This will create the 0001_initial.py file within your app's migrations directory.
You will not be able to actually migrate the initial database migration because the database table already exists, so you will need to fake the migration with the command, python manage.py migrate my_app --fake-initial
Instead of run:
python manage.py migrate
just run:
python manage.py migrate --run-syncdb
If you have many problems and require a solution
Create a database backup (more than one if necessary)
My solution was to run the following query in the SQL shell
drop database database_name with (force);
This completely removes the database, be careful
It solved the error for me
Greetings
After running into this problem on Django 3.0 I spent an entire day troubleshooting this. Sharing this so that others would not need to. Most of the times running
python manage.py makemigrations
python manage.py migrate
Should solve the problem. I followed the above step after deleting the files in migrations folder (except init.py), however I was not able to solve the problem. I found that every time you run makemigrations, a python file called 000x_xxx.py is created in the migrations folder. I looked inside the file and found that all my required changes were there but it also had other create table operations that I did not need (i.e those tables had already been created). If you run migrate with --fake or --fake-initial, the whole execution will be faked (i.e executed without actually making any change in the database).
To solve the problem I opened the generated 0000x_xxx.py file and deleted the changes I wanted to make in the database and ran "python manage.py makemigrations" again. This created a new migration file with the changes I wanted. After that I ran the following command.
python manage.py migrate --fake-initial appname
This solved the problem. Not sure if this should be this complicated.

can't delete old south migrations from heroku

I made many changes in my django app and when working locally i reset the database, deleted the migration and reseted south, recreated everything all over and it worked fine.
when trying to do the same in Heroku,
after droping the database using:
heroku pg:reset DATABASE
and also reseting south using:
heroku run ./manage.py reset south
and then pushing the Django app when looking at the heroku migration list using:
heroku run ./manage.py migrate --list
i can still see all of the old imgration,
though they are empty-- no * in the ()
so even after doing
heroku run python ./manage.py migrate accounts --fake
i still get migration errors:
DatabaseError at /admin/accounts/userprofile/
****relation "accounts_userprofile" does not exist
LINE 1: SELECT COUNT(*) FROM "accounts_userprofile"****
what can i do to solve it? is there a wayy to remove old migrations in heroku and just do it simple like when working locally by starting all over again?
or is their any other solution?
--fake is used when the database tables already exists and you no longer need to actually migrate the real database. As the tables don't exist, you should just run migrate without --fake.