Problems with manage.py in django - python

I am attempting to run a site from my localhost. What I did is I first cloned the repo and pull it into my local directory. Then, I changed the DB part to my DB settings. I have tried python manage.py runserver, python manage.py migrate and python manage.py makemigrations, but it still shows the same error.
error
I have tried different solutions found online, but they don't seem to work. I think the main problem is that I'm not sure where the problem is. I have tried creating a superuser, but it still shows the same error.

Related

django.db.utils.OperationalError: no such table: blog_category

I keep getting the error when i try running python3 manage.py makemigrations and python3 manage.py migrate
i have checked over a lot of posts and they all say to run python3 manage.py makemigrations and python3 manage.py migrate or even python3 manage.py migrate --run-syncdb but the problem is that i get this error when i run those commands as well as when i run python3 manage.py runserver:
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: blog_category
the other problem is that i don't every have any code that says blog_category so i don't even know where django is getting that from. i'v tried different venv's to see if other installed programs were the problem but the error persists. Please help.
I had this problem too. I was unable to make new migrations even after manually deleting the migrations. I noticed that my db.sqlite3 file turned into a text file after forking my project from github.
Here's what you can do:
Ensure that your db.sqlite3 file in your project root folder has the extension ".sqlite3", it should not be without a file extension. Also make sure that it is not an empty file (0 KB).
You should not be able to open the db file and see this:
This is not what should be. The way around fixing such an issue is to copy the db.sqlite3 file over from another project (as it will keep generating a false one otherwise). Then make some changes to admin.py or models.py so that the database will realize it needs to do something (make migrations), and then luckily - you should now be able to make migrations (python manage.py makemigrations).
If the above does not provide resolution, this is how you can completely reset the database:
python manage.py flush
Create the superuser again and make any necessary migrations:
python manage.py createsuperuser
python manage.py makemigrations
python manage.py migrate
It is also important to ensure that you have no active code in your project which would try to query data from a database table which no longer exists.
Realize that you now have an empty database.
If I would have code running in the project that would try to retrieve data from a database object that used to be there in the past, something like:
p = Category.objects.get(title=flower_type)
print(p.flower_set.first())
print(p.flower_set.last())
It would cause problems, since there are no database entries, and there is no data to query. It's something to watch out for.

Why do I fail Django migration in heroku server while succeeding in local server?

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.
python manage.py makemigrations
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 : git add .git commit -m "migration" git push heroku master heroku run python manage.py migrate app_name
But then I got this error CommandError: App 'app_name' does not have migrations.
Strange thing is, after running python manage.py migrate, everything works fine in my local server. It's only the heroku server where the error occurs.
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 but neither worked.
I'm very confused. What do you think is the problem? Thanks in advance. :)

Why do I get "CommandError: App 'app_name' does not have migrations." when using Heroku?

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.

DJANGO HEROKU :django.db.migrations.exceptions.InconsistentMigrationHistory:

i have pushed my django application to heroku
and when i tried to fireup the url where my application is
i just got this error
ProgrammingError at /
relation "accounts_user" does not exist
LINE 1: ..."."is_active", "accounts_user"."date_joined" FROM "accounts_...
so i have figured out that the issue is with migration so i tried to run the makemigrations command in heroku
but unfortunately it returned as a failure with an error message
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration posts.0001_initial is applied before its dependency accounts.0001_initial on database 'default'.
so what i have tried is i have completely removed the migrations and database and runnned makemigration and migrate command in my local server and then i pushed it back to heroku but still its of no use
my local is running perfectly fine
i'm not sure where exactly the migrations issue is , in my heroku django application
i want to remove the previous migrations and have a clean database
but i have no idea on how to do that
I was facing the same issue yesterday. I was using Django rest and sqlite3 DB.
To solve that error I have clear all migration and delete a migration folder and clear DB file. After that, I have run migrations specific for that module and then run migrate command then it works fine for me. I hope it will work for you as well.
e.g.
$ python manage.py makemigrations module_name
$ python manage.py migrate module_name

Heroku and Django Running Server

I'm following this tutorial: http://tutorial.djangogirls.org/en/domain/README.html
When I do python manage.py runserver it works fine. It also works when I run
heroku ps:scale web=1
then
heroku open
with python manage.py runserver it shows my blog posts and everything that I had added. But when I run the server with heroku open there are no posts as if the database is missing or something.
Why is that? Why do the two commands launch the same web page but with different posts/different databases?
Which brings me to my follow up question: how do I know when I need to run migrate or makemigrations again for the server? Would doing so fix the problem? And what exactly do those commands do/why are they necessary?
Thanks
EDIT:
Bonus question: Why do my posts display in descending order of time? The new posts are on the bottom of the page rather than the top. How can I change this?
There is a difference between your local development and your deployed project.
I assume you created your posts local. So they are saved local into your database. Local you use a filebased database defined in the settings 'django.db.backends.sqlite3' that means when you run manage.py syncdb the file is created with all tables inside. When you deploy your code to heroku the code is pushed to the server and runs from this place. This can be everywhere, so it can't connect to your local databasefile. For your project you has to setup a database on heroku as well. I recommend to read this article. When you want to transfer your data you can create a database dump local and load all data to you heroku database. Described here and here.
Another point, you don't run the server with heroku open or by fire python manage.py runserver. The heroku server is automatically started when your git push heroku master is done. It use the configs from your Procfile.
When you want to migrate your heroku database you has to run heroku run python manage.py migrate <app_name> than the migration is done remotely on the heroku server. You have to run this command everytime you changed a model and added a migrationfile with python manage.py makemigration <app_name>. When you did this you has to migrate you database local and remote. That means that you change the database structure to match your models. Remember the models are only a abstraction(orm) of your database.
I don't know your project but the order sees legit. Try to imagine this as rows. first row comes first. So the last entered row is on the bottom. You can change the order of the queryset with something like.order_by('-id'). So you get all entries in reverse order.

Categories

Resources