django doesnt run a specific migration (it skipped) in production - python

when I ran the makemigrations it returns
users/migrations/0002_remove_profile_image.py
- Remove field image from profile
But when i ran migrate users 0002.. it said CommandError: Cannot find a migration matching '0002_remove_profile_image.py' from app 'users'
when i ran migrate it said
No migrations to apply.
Your models in app(s): '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.
what can i do?
i am deploying on heroku

It seems you are using the migration name with the .py:
./manage.py migrate users 0002_remove_profile_image.py
But the correct usage is without it, so just remove that:
./manage.py migrate users 0002_remove_profile_image
or just the short-hand:
./manage.py migrate users 0002

Sorry, my bad. I was running with heroku run I should have run locally first.

Related

Django and pgAdmin not aligned

I was trying to change the order of the table columns in a postgreSQL table. As I am just starting, it seemed easier to manage.py flush and create a new DB with new name and apply migrations.
I can see the new DB in pgAdmin received all the Django models migrations except my app model/table. I deleted all migrations folder (except 0001_initial.py) and still, when I run python manage.py makemigrations I get:
No changes detected
But the model class table is not in the DB. When I try to migrate the app model it says:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
Is there any way to delete all tables, makemigrations and migrate and postgres get all the tables? Any idea why postgreSQL/pgAdmin cannot get my Django app model/table?
You can do a particular app migration using below commands:
python manage.py makemigrations your_app_name
python manage.py migrate your_app_name
This works for me.
The app specific migrations in my case were not working.
python manage.py makemigrations your_app_name
python manage.py migrate your_app_name
What did the trick is to modify the models adding an additional fake variable, then all the variables (and not only the additional one) gets picked up by the makemigrations and migrate command. It is not a clever solution but it did the trick.

Migrations in DJango

I am having DJango migrations problem while making migration following error is coming.
When I run my applications using python manage.py runserver it shows this :-
However, running python manage.py makemigrations shows no changes detected
And Above three images are result after running python manage.py migrate.
What is the problem with this?
When the *table> already exists Error happens, it is usually due to deleting and rerunning the initial migration or models.py file. For these scenarios,
python manage.py makemigrations <app_name>
python manage.py migrate --fake-initial <app_name>
Or if you want to fake only one migration file
python manage.py migrate <migration_file_number> --fake <app_name>
--fake-initial tells Django to mark initial migration as migrated without actually running its corresponding SQL.
Django's migration document may be helpful
looks like you changed the database or migration files manually.
try to re-create the database.
delete the DB file
delete all migrations files (keep the init file)
run create migrations command
run migrate command

Relation does not exist error in Django

I know there are many questions about this problem, I looked through the solutions and unfortunately none of them worked for me.
I created a new app called "usermanagement", and added a model to the app. After adding the model I added usermanagement to INSTALLED_APPS in settings. Then I ran python manage.py makemigrations, and python manage.py migrate. This all worked fine! I also did try running the migrations with the app-name.
The problems start when I try to add a new instance of the model to the database in the Python-Django shell, by using:
>>>a = ClubOfficial(name="randomName", email="randomemail#random.com")
>>>a.save()
I get the following error:
django.db.utils.ProgrammingError: relation
"usermanagement_clubofficial" does not exist
LINE 1: INSERT INTO "usermanagement_clubofficial" ("name", "email") ...
Below is the model code:
class ClubOfficial(models.Model):
name = models.CharField(max_length=254)
email = models.EmailField(max_length=254)
If it helps, I use postgresql, and have tried restarting the server. The other apps in the program also work perfectly fine, it is just usermanagemenet that has this problem.
Does anyone know what could be going wrong?
Thank you for your time!
Note: I created a new app now with a different name, copy-pasted things from usermanagement and everything worked fine. I think the problem might be that before there was an app named usermanagement which was deleted, before I created it again. Maybe that messed up the database somehow.
TL;DR: Make sure your app's migrations folder has an __init__.py file. If it isn't there, create it again as an empty file.
I ran into this. In my case I had a previously working django app, not yet moved to production, so I deleted everything in my app's migrations folder, then using django extensions I wiped the postgresql database and cached files with:
./manage.py clear_cache
./manage.py clean_pyc
./manage.py reset_schema
./manage.py reset_db
# then deleted all files (including __init__.py) from my app's migrations folder.
I verified that my postgresql database had no tables. I then ran:
./manage.py makemigrations
./manage.py migrate
Which gave the output:
No changes detected
./manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
(about 11 more lines of output here which are similar)
It is notable that my model's names where nowhere in the migration. I printed the tables in my postgresql database and the Widget and Section tables were both missing. Running the app gave me this error (I substituted 'app' and 'model' for their real names):
ProgrammingError at /my_path
relation "app_model" does not exist
LINE 1: ..."."my_field", "app_model"."my_field" FROM "appname...
So the tables for the models were not being created in the database. My app's migrations folder was also completely empty. The solution was to just add an empty __init__.py to my app's migrations folder, and after that running makemigrations was able to create and save the migration file in the folder. This could then be executed with migrate. You can test this for yourself by running makemigrations with and without the __init__.py in the migrations/ folder.
This solution was not mine but one given by user Ljubitel on another post but it was not the accepted answer there, but the accepted answer didn't work for me so I wrote this solution here to hopefully help others.
I had this same problem, but all I had to do was run
$ python manage.py makemigrations
and
$ python manage.py migrate
In case you have deleted your migration folder.
Create a folder called migration in whatever app name you have created and then create a file in the migration folder called __init__.py
In my case I was pointing to a different databases between my local server and the production server. The database that the production server was pointing to was a few versions behind, so the server could not locate the relation. If your issue were localized to one environment, check the configs first.
Another method to fix relation does not exist error
Create same table in db(postgre, mysql) using sql query tool
now comment your model in models.py and admin.py
run migration using :
python manage.py makemigrations app_name
python manage.py migrate
now uncomment and run migrations command again
I encountered same issue and fixed using following method, I am using postgres(pgadmin 4).

How to migrate the local database on heroku with new tables?

I get the error:
Exception Value:
no such table: hello_surname
when i try to show a view that accesses the Surname model
in my models.py
class Surname(models.Model):
created = models.DateTimeField('date created', auto_now_add=True)
name = models.CharField(max_length=35)
I've tried to run the migrate $ heroku run python manage.py migrate
ouput:
Running python manage.py migrate on ⬢ sleepy-fjord... up, run.7027
Operations to perform:
Apply all migrations: sessions, auth, hello, contenttypes, admin
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.
then i run $ heroku run python manage.py makemigrations
output:
Running python manage.py makemigrations on ⬢ sleepy-fjord... up, run.8567
Migrations for 'hello':
0002_surname.py:
- Create model Surname
when i run the top migrate again, it just gives the same output as when i ran it the first time. Seems like I am missing a step here, but with the output i can't seem to figure it out by myself. Anyone know any solutions?
You must run make migrations locally, commit the results, then run migrate on Heroku.

South 'nothing to migrate' message after successful schema migration

I have a Django app that I added South to, performed some migrations with, and runs as expected on my local machine. However, I have had nothing but database errors after pushing my project to Heroku.
In trying to deal with one database error I am experiencing, I attempted a test where I deleted one of my models, pushed the edited models file to Heroku and ran:
heroku run python manage.py schemamigration django_app test_remove_pub --auto
This seemed to work fine. I got back the message:
Running `python manage.py schemamigration apricot_app test_remove_pub --auto` attached
to terminal... up, run.6408
- Deleted model django_app.Publication
- Deleted M2M table for journalists on django_app.Publication
- Deleted M2M table for tags on apricot_app.Publication
Created 0006_test_remove_pub.py. You can now apply this migration with: ./manage.py
migrate django_app
So, South appeared to do everything I expected - it deleted my model and its many to many relationships and made the appropriate migration file. Next, I enter:
heroku run python manage.py migrate django_app
And I get back:
Running `python manage.py migrate django_app` attached to terminal... up, run.4792
Running migrations for django_app:
- Nothing to migrate.
- Loading initial data for django_app.
Installed 0 object(s) from 0 fixture(s)
Why would it say "nothing to migrate" when obviously there are things to migrate??
After you change the models, you need to do :
heroku run python manage.py schemamigration django_app --auto
if anything has changed
and then run
heroku run python manage.py migrate django_app
South applies migrations based on the entries in this database table: south_migrationhistory. So if you want to manually override it,
Remove all the entries with column app_name of the models you changed
Manually remove all the related tables. You can get a list of all the tables by typing this in the django shell:
from django.db.models import get_app, get_models
app = get_app(app_name)
for model in get_models(app, include_auto_created=True):
print model._meta.db_table
Remove the migrations/ folder related to the app
Do a fresh migration: ./manage.py schemamigration app_name --initial
Apply the migration ./manage.py migrate app_name

Categories