I deleted two models from models.py, and when I run makemigrations and migrate locally, everything is fine. When I run makemigrations on Heroku, I get the following message, where Building and BuildingInstance are the models I deleted:
Migrations for 'hello':
0002_building_buildinginstance.py:
- Create model Building
- Create model BuildingInstance
When I run migrate, I get:
Running migrations:
No migrations to apply.
Your models 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.
I followed the steps here and also tried squashing the migrations locally and on Heroku as suggested here. How can I fix this problem?
As I have written many times here, you must not run makemigrations on Heroku. Run it locally, commit the result, and then run migrate on Heroku.
Related
So I have deployed my Django project to Heroku, and now trying to migrate the database. I have everything working fine in my local sever. Then I tried to run the commands in heroku, as below.
heroku run python manage.py makemigrations app_name'.
This worked fine.
Migrations for 'app_name':
contents\migrations\0001_initial.py
- Create model Book
- Create model Category
- Create model Content
Then of course, I tried : heroku run python manage.py migrate app_name.
But then I got this error CommandError: App 'app_name' does not have migrations.
I've done some research for possible issues, none of which were relevant to mine.
For example I do have __init__.py in the migrations folder inside app_name directory. Also, I've tried heroku run python manage.py migrate as well as heroku run python manage.py migrate app_name. I'm very confused. What do you think is the problem? Thanks in advance. :)
I had the same problem, I don't know why, but the following works for me (two commands in one line):
python manage.py makemigrations && python manage.py migrate app_name
As explained in heroku's help article Why are my file uploads missing/deleted?:
The Heroku filesystem is ephemeral - that means that any changes to
the filesystem whilst the dyno is running only last until that dyno is
shut down or restarted.
That is you do generate the migration files but they cease to exist shortly after when the app is reloaded.
In general the strategy you take is not a good approach to migrations as this can cause various issues, let's say you have multiple production servers each generates their own migrations this causes a conflict! In your situation suppose you do migrate this way and then later you want to add some new models this again will cause you problems.
One should always add their migrations to the version control (git, etc.) commit them and push them to production (here your heroku dyno). That is run python manage.py makemigrations locally, commit the generated migrations, push them and then run heroku run python manage.py migrate.
I am new to Django. I want to create tables by using models.
I have tried to follow this link: https://docs.djangoproject.com/en/3.0/topics/db/models/.
I have registered the app and provided connection details of the DB.
But when I tried to run makemigrations and migrate, it's not identifying the changes.
python manage.py makemigrations
No changes detected
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
any help would be appriciated.
For a new app, one without any migrations, you must first create an initial migration file. This can be done by passing the app name to makemigrations
python manage.py makemigrations app
Here is the relevant section of the docs
To add migrations to an app that doesn’t have a migrations directory, run makemigrations with the app’s app_label
I've created a new Django project consisting of a single app. I thought Django would only migrate an app only if its migrations exist.
The first time I run python manage.py migrate, Django would create tables for my app as well(I've created models.py for my app)
I haven't run python manage.py makemigrations app or python manage.py makemigrations till this time.
I would get the following output on running python manage.py migrate:
Synchronizing apps without migrations:
Creating tables...
Creating table app_model
This is not what I want, because the next time I actually make migrations for the app and run migrate, Django would complain that the tables already exist.
You can create the migrations, and fake the first migration:
python manage.py makemigrations <app_name>
python manage.py migrate --fake-initial
This will skip any initial migration for which the tables have already been created.
Django (pre 1.9) will synchronize any apps that don't have migrations, see the docs on migrate:
The behavior of this command changes depending on the arguments provided:
No arguments: All migrated apps have all of their migrations run, and all unmigrated apps are synchronized with the database,
If you want to save migrations for app, wich you already migrate. You can change one field in model, then makemigrations this_app -> migrate and return has been again with makemigrations and migrate. But I dont understand why you want this.
I mistakenly deleted all the .py file under path projectName/appName/migrations, it include like:
0001_initial.py
0011_auto_20150918_0723.py
0002_auto_20150819_1301.py
...
Now, even I updated the Model file, and run the command python manage.py makemigrations, it always prompt with No changes detected.
How can I recover everything?
First of all, even if this happened to you in production, do not panic.
When you deleted all migrations django forgot that this app is supposed to be managed by migrations. Django defaults back to the legacy python manage.py syncdb migrationless behaviour and will not attempt to detect changes or generate new migrations when you run python manage.py makemigrations
In order to make it aware of the migrations, you have to run the command specifically for your app:
python manage.py makemigrations appName
However, for your running application, django will not be able to detect that the new migrations were already applied in the database, and will try to run them again when you run python manage.py migrate.
When this happens the migrations fail saying that the relation appName.XYZ already exists!.
To make django understand that your migration is already reflected in the database you have to fake them:
python manage.py migrate appName --fake
This will update the migration history table and mark your migrations as applied, but will not attempt to create the tables in the database.
Update (thanks to #GwynBleiD):
One concern here is that deleted migrations which were already applied, will be listed in the migrations history table in the database. This will not be a problem for the initial python manage.py makemigrations myApp, however for any other migrations it will raise an error about inconsistent migration history.
To avoid that, you must remove by hand any row from django_migrations table in database that refers to nonexistent migrations.
I had a app where there were models and that site I have deployed. Now I have added 2 models in the app. Unfortunately my migration was deleted in local and after adding the models I again generated the migration with 2 new models.
It gives me 0001_initial.py where I can see my new models too. Now I updated my this file in server. But when I did python manage.py migrate it says "Some of your model has been changed but not reflected so do manage.py makemigrations and manage.py migrate"
I tried "python manage.py migrate" but it says same thing. I am unable to get these models. I have even tried "python manage.py syncdb" but same error.
I have registered these models in admin so in admin I am seeing these models but when I click it my servers get hang. I think because my models are not synchronised or my new tables are not created.
In my migration i can see the new models too but when I do "manage.py migrate" i get errors that I should migrate first and re-migrate.
Is there anyway so that I can compel my app to use that migration forcefully where I have my new models too.
I am trying this from hours but no solution. Need help