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

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.

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.

psycopg2.errors.UndefinedTable: relation "django_admin_log" does not exist

I just started learning Django, and I'm following a book as guide (the book is from August 2022, so new) and I ran into 2 problems.
The first one was that Python couldn't find the module psycopg2 which I then installed. Now I'm a little further and created my first model and migrated it to the database, which all seemed to work well. I then created a superuser and opened localhost:8000/admin/ and it sent me to the admin site, I logged in with my newly created user, so far so good. Now the problem.
This is what the site shows me:
And this is what the log says:
I've tried many approaches I found on here, for example deleted the migrations folder in my applications folder and then migrated my application again. I'll just go through a few other commands I've tried:
>> python manage.py migrate --run-syncdb admin
#CommandError: Can't use run_syncdb with app 'admin' as it has migrations.
>> python manage.py sqlmigrate admin 0001
# response: [The SQL query...]
>> python manage.py syncdb
# response: Unknown command: 'syncdb'
>> python manage.py migrate --fake
#Operations to perform:
# Apply all migrations: admin, auth, blog, contenttypes, sessions
#Running migrations:
# No migrations to apply.
This is what the database looks like right now
I found the answer myself on https://www.pythonanywhere.com/forums/topic/14632/
I first ran the command python manage.py migrate admin zero --fake, and then migrated again with python manage.py migrate, then ran the server and now the error is gone!

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

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.

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

How to apply a particular django database migration in heroku?

I uploaded my modified code with some changes in models. When I run heroku run python manage.py migrate app to apply the database migrations it gave me an error
CommandError: Conflicting migrations detected (0004_auto_20150819_0827, 0008_auto_20150813_1444 in app).
To fix them run 'python manage.py makemigrations --merge'
So when I run heroku run python manage.py makemigrations --merge it gave me output:
Created new merge migration /app/app/migrations/0009_merge.py
Now how can apply this migration to my database ?
Maybe
heroku run python manage.py migrate
or to see what it's going to apply:
heroku run python manage.py showmigrations

Categories