Django on Heroku won't let me create a superuser - python

I've deployed a django app on to heroku. This app works perfectly running locally. However when I try to create a superuser on the heroku app so that I can access the admin page things aren't working. I've run:
heroku run python manage.py syncdb
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
And it appears to work. I get the prompt to choose a password, email and username. It then states:
Superuser created successfully.
However when I try to logon in the admin panel the username password combo fails. When I try to change the password of the account through:
heroku run python manage.py changepassword admin
I get:
CommandError: user 'admin' does not exist
Evidently heroku never actually created a supersuer. I'm at my wits end here, I don't understand what's wrong with the heroku dyno.

Just running heroku run python manage.py createsuperuser worked for me. But #Nils , I want to know which line in the settings.py file you had to tweak to make it work for production.

It looks like you are using a fuse system base database like sqlite.
As you know, Heroku has an ephemeral filesystem:
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. Each dyno boots with a clean copy of the filesystem from the most recent deploy
This is the reason why create user sun's successfully but, on next command (changing passed) the user is not there.
I write this answer mostly for future users with the same problem.

To check what's going on, try running the following line twice:
heroku run python manage.py migrate
heroku run python manage.py migrate
On a good setup, the first time will run the migrations, and the second time will say "No migrations to apply". If you see it run the same migrations both times, this means that your database in Heroku didn't persist from one call to the next, and you need to change your database setup, for example by connecting a postgres database.

Related

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.

Why isn't Heroku's Postgresql updating according to Django's models.py?

Currently I'm running into issues with my models.py and Postgresql. The site is deployed on Heroku using Postgresql and the problem is models.py. Currently, I have models.py as a models.Charfield() and it works fine with sqlite, but when I deployed it to production, Postgresql still reports that it's a models.IntFied(). And because it's an integer field it won't allow for characters for some reason.
Originally, I had it as a integerfield but decided to change to charfield, yet it still won't change to charfield.
Solutions I've tried: resetting the database on heroku, deleting and recreating the database on heroku.
Any help is appreciated,
Thanks
To sync your models to your database relational schema, you have to run the command:
python manage.py makemigrations
python manage.py migrate
I normally follow the steps in this order:
Ensure that your database changes have all been committed with git add and commit commands
Reset the heroku database, Run
{heroku pg:reset DATABASE_URL}
Push your current changes to heroku with
{git push heroku master}
Launch the heroku terminal
{heroku run bash}
On the heroku terminal 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.

Add a column to heroku postgres database

So, locally I've changed my models a few times and used South to get everything working. I have a postgres database to power my live site, and one model keeps triggering a column mainsite_message.spam does not exist error. But when I run heroku run python manage.py migrate mainsite from the terminal, I get Nothing to migrate. All my migrations have been pushed.
Get the list of migrations available for your apps (it will mark what are pending and what others are migrated).
heroku run python manage.py migrate <your app name> --list
if you have migrated with --fake, then identify the number of migration is pending (0003, for example) then do a revert of migrations to get the the previous state:
heroku run python manage.py migrate <your_app_name> 0002 --fake
now try again to migrate.
heroku run python manage.py migrate <your app name>
Did you run schemamigration before? If yes, go to your database and take a look at your table "south_migrationhistory" there you can see what happened.
If you already did the steps above you should try to open your migration file and take a look as well, there you can find if the creation column is specified or not!
I presume that you have created a migration to add mainsite_message.spam to the schema. Have you made sure that this migration is in your git repository?
If you type git status you should see untracked files. If the migration is untracked you need to git add path_to_migration and then push it to Heroku before you can run it there.

Categories

Resources