My django code is requesting a table that doesn't exist - python

I tried to delete the 'db.sqlite3' file and do the migration again, but it didn't work

If you made changes to your model, you need to run python manage.py makemigrations. If this is a new project that you have never deployed to production, you can delete the migrations folder before running this command.
Then run python manage.py migrate.

Related

I can't make migrate back, after I deleted them in Django?

I am using Django with VSC.
I got some problem with my database, so i did want to delete inside of migrations but by mistake
i deleted directly folder of migrations. Anyway then i used the code;
python manage.py makemigrations
python manage.py migrate --fake
Then i got my migrations informations back. But the problem is, this time it didn't detect
my own migrations. So when i click something have connection with database. I got a mistake like
"No such table: MyMigrationsName"
How can i fix it ?
OP can create there a folder named migrations in the place OP deleted and an __init.py__ file inside of it.
Then, if possible, drop the current database and run
python manage.py makemigrations
python manage.py migrate
Right now you can DROP database, delete all migration files, run makemigrations and create a new database with the new migration files.
Then never make the same mistake again.
Try:
python manage.py makemigrations "your_app_name"
py manage.py migrate "your_app_name"'
Make sure the app name is included. This solved my problem.

Django Heroku and postgresql

I am new to publishing django apps on heroku.
I have read their tutorials and figured I would start simple by modifying their template django project.
So far everything was good.
Then I made an app as one does, made a model and ran
python3 manage.py makemigrations my_app
python3 manage.py migrate
and things seemed ok.
I then pushed to heroku and there were no immediate complaints.
However, now when I try to make save a new model I get the error:
ProgrammingError at /my_app/
relation "my_app_myappmodel" does not exist
LINE 1: INSERT INTO "my_app_myappmodel" ("field1", "field2", "field3") VALUES...
odd...
So I run this locally and everything works fine.
I have tried cleaning my migrations, faking my migrations, squashing my migrations, etc (as other S.O. posts suggest)
Nothing works.
What is up and how do I fix it?
You need to actually run the migrations on Heroku once you have pushed the code generated by makemigrations. You do this via heroku run manage.py migrate.
run the following command from your terminal
heroku run python manage.py migrate
or you can also do:
in your local settings.py, change your DATABASES variable to use the heroku one then run from the terminal
python manage.py makemigrations
python manage.py migrate
but you should not normally locally make changes to the heroku production database (as in option 2) except if you are really desperate or don't care

Is it possible run makemigrations with deleted schema?

My all SQL schema was deleted cause of windows10 initialization.
So, I wanna run makemigrations with already existing django project code(with so many lines......).
But "python manage.py makemigrations" not works with error 'Table doesn't exist'... Is there some methods overcome these situations ???
Your existing migrations scripts are corrupt. So you need to do the following steps:
Delete the migrations folder inside your app
Then run makemigrations
python manage.py makemigrations
Apply the changes to DB
python manage.py migrate

Django model changes cannot be migrated after a Git pull

I am working on a Django project with another developer. I had initially created a model which I had migrated and was synced correctly with a MySQL database.
The other developer had later pulled the code I had written so far from the repository and added some additional fields to my model.
When I pulled through his changes to my local machine the model had his changes, and additionly a second migration file had been pulled.
So I then executed the migration commands:
python manage.py makemigrations myapp, then python manage.py migrate in order to update my database schema. The response was that no changes had been made.
I tried removing the migration folder in my app and running the commands again. A new migrations folder had been generated and again my database schema had not been updated.
Is there something I am missing here? I thought that any changes to model can simply be migrated to alter the database schema.
Any help would be greatly appreciated. (Using Django version 1.9).
After you pull, do not delete the migrations file or folder. Simply just do python manage.py migrate. Even after this there is no change in database schema then open the migrations file which came through the git pull and remove the migration code of the model whose table is not being created in the database. Then do makemigrations and migrate. I had this same problem. This worked for me.
In your ProcFile add this line at the beginning.
release: python manage.py migrate

How to make South works in Heroku for a Django app

I am working on Python/Django and I am trying to use South to manage my database. In local environment is working great. The problem comes when I deploy to Heroku. The issue is that when I create a migration with
$heroku run manage.py schemamigration mydjangoapp
It looks like it works (the shell confirmed it), however, then I try to use migrations and it won't work. When I do:
$heroku run python manage.py migrate mydjangoapp
I get this;
The app 'createtuto' does not appear to use migrations
I checked on the problem and it looks like heroku doesn't allow South to create the migration directory at /myDjangoapp/migrations.
Is there anything I can do to make it work?
I tried to use convert_to_south, but I got the same results: At the beginning it looks like it worked, but It did not, not migration created.
When you run 'heroku run' it connects to an isolated instance of your deployed environment. It does create the migration, however that migration is not contained within your slug. Each time you do a 'git push heroku master' it installs your dependencies and packages your application into a slug. This is more or less a tarball of your application which enables Heroku to easily deploy it to new dynos as you scale up.
In order to run a migration on Heroku you would create the migration locally, check it in, then run the migration on heroku. Something similar to:
manage.py schemamigration mydjangoapp
git add mydjangoapp/migrations/*
git commit -m 'adding new migrations'
git push heroku master
heroku run python manage.py migrate mydjangoapp
I follow the direction from Mike Ball here successfully:
http://www.mikeball.us/blog/using-south-on-heroku/
Like CraigKerstiens answer said, you should make the migration locally first then push to heroku. Before you make a migration on Heroku, make sure you convert your Heroku instance into south, for example
heroku run bin/python django_project/manage.py convert_to_south django_app

Categories