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?
Related
I'm trying to implement Google Login on my Django production website. It works fine on development server, but on my production server, I face this issue after having run python manage.py migrate. I'm not sure what to do, I already tried deleting all the migrations and re-running makemigrations and migrate.
Checklist
run showmigrations to see which migrations are done.
if the relevant migration containing the table appears to be migrated, rollback and run migrate again.
when you want to regenerate migration files, you need to first rollback while you have the old migration files.
if you'd like to check which tables exist, use dbshell or other shell depending on the database you're using.
Other things
I would not recommend regenerating migration files just because there is some db issue.
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.
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!
I deployed a new django app on Heroku which worked out fine. However, today I changed my model a little bit (added a new field) and afterwards deleted my development server file db-sqlite3 and the migrations folder in order to reflect the changes in the development database. This worked out fine on the local server but when I pushed this to heroku I get a 500 error when trying to access the model in the django-admin section.
I tried to run some migrations via heroku but I get the following error message:
! These migrations are in the database but not on disk:
<joins: 0002_auto__add_field_join_ip_address>
<joins: 0003_auto__add_unique_join_email>
<joins: 0004_auto__add_field_join_ref_id>
<joins: 0005_auto__add_unique_join_email_ref_id>
<joins: 0006_auto__add_unique_join_ref_id>
<joins: 0007_auto__del_unique_join_ref_id>
<joins: 0008_auto__del_unique_join_email__add_unique_join_ref_id>
<joins: 0009_auto__add_field_join_friend>
! I'm not trusting myself; either fix this yourself by fiddling
! with the south_migrationhistory table, or pass --delete-ghost-migrations
! to South to have it delete ALL of these records (this may not be good).
(lwc) Daniels-MacBook-Pro:src danielrichter$ heroku run python manage.py migrate --delete-ghost-migrations
I can see that in my local migrations folder I only have the 0001_initial migration and somehow missing the other but I have no idea how to resolve the issue.
I have seen that others ran into the same error message, but I did not understand the proposed answers, since I am quite new to Django and coding in general. So if there is someone who could give me a hint how to resolve this I would be very thankful!
many many thanks!
Apparently the database thinks you have applied the migrations mentioned in the message, but it can't find the files on disk. Which you confirm. Maybe there was something wrong with your version management and you lost these files? I'd try to see if you can recover the files, then the problem would be over.
If not, it is a bit more difficult. The migrations mentioned have probably already been executed but the files are lost. Also you have made new changes which have not been applied. You should try to get your code state back to the state where the last missing migration was executed. You can then make a new migration file (python manage.py schemamigration --auto your_app_name) which can replace the missing migration files. This migration will be called 0002_something (0001 being present and 0002 being the next). After this replacement migration you can have new migrations (0003 and further).
On the server, before updating your version of the code (so you don't already have 0002) execute python manage.py migrate --delete-ghost-migrations. This will delete the references to the missing migration. Afterwards you can update your version and get the new 0002 etc migrations.
Call python manage.py migrate --fake your_app_name 0002. This will tell the database that the migration was applied, without actually applying anything. This is good because the changes were already applied by the lost migration files.
After this, you can run normal migrations: python manage.py migrate and it should be good.
Hope this helps.
and afterwards deleted my development server file db-sqlite3 and the
migrations folder in order to reflect the changes in the development
database
This is your issue, you shouldn't delete any migration files, if you make a change in your database, django (or south) will create a new migration file for those changes, then you run the migration command to apply those changes to your database, you have to commit those new migration files and send them to heroku, so the changes will be applied to the remote database too.
The workflow is like this:
You have some migration files or the initial one
Edit your models by adding/removing fields
Create migration file(s), in django (without South):
python manage.py makemigrations
Apply those changes to your local database by runnig:
heroku run python manage.py migrate
Add those model changes and migration files to your git index
Push the changes to heroku and run the migration command:
heroku run python manage.py migrate
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.