Forums

djangogirls/pythonanywhere: database migrations, creating databases

The directions at djangogirls for creating databases on pythonanywhere did not work for me:

Creating the database on PythonAnywhere

Here's another thing that's different between your own computer and the server: it uses a different database. So the user accounts and posts can be different on the server and on your computer.

We can initialise the database on the server just like we did the one on your own computer, with migrate and createsuperuser:

(myvenv) $ python manage.py migrate
Operations to perform:
[...]
  Applying sessions.0001_initial... OK


(myvenv) $ python manage.py createsuperuser

This is what I got:

(djgirls)05:05 ~/my-first-blog (master)$ python manage.py migrate
Operations to perform:
  Apply all migrations: auth, contenttypes, sessions, admin, blog
Running migrations:
  No migrations to apply.
(djgirls)05:06 ~/my-first-blog (master)$

Note how the last line says no migrations were applied.

According to the help directions at pythonanywere, you have to go to your pythonanywhere dashboard, then click on the databases tab, then select the type of database you want to create, then create a password. After you create your password, you will see a database called:

<your_user_name>$default

You can use that database, so you don't need to create a new database.

Then for python3 you need to use pip to install mysqlclient (noted at the end of the help directions). If you look at dashboard/consoles tab, there is a link you can click on to open a bash shell (or you can use a previously opened shell):

08:14 ~ $ ls
README.txt  my-first-blog

08:14 ~ $ cd my-first-blog/
08:15 ~/my-first-blog (master)$ ls
blog  db.sqlite3  djgirls  manage.py  mysite  static

08:15 ~/my-first-blog (master)$ source djgirls/bin/activate  (make sure your venv is activated)
(djgirls)08:15 ~/my-first-blog (master)$ pip install mysqlclient

The pythonanywhere help directions use virtualenv, which is a third party python module for creating virtual environments. It is different from venv, which is builtin to python. The djangogirls tutorial uses venv, but switches to using virtualenv on pythonanywhere.

Finally, you need to make some changes to settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<your_username>$<your_database_name>',
        'USER': '<your_username>',
        'PASSWORD': '<your_mysql_password>',
        'HOST': '<your_mysql_hostname>',
    }
}

The fill in for <your_mysql_hostname> can be found in the databases tab on your dashboard.

After doing all that, the migration worked:

(djgirls)08:15 ~/my-first-blog (master)$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, blog, auth, contenttypes, sessions
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying blog.0001_initial... OK
  Applying sessions.0001_initial... OK
(djgirls)08:37 ~/my-first-blog (master)$

Hi there,

Is it possible you accidentally committed your sqlite database into your git repo?

The djangogirls tutorial assumes you're using a sqlite databse, which is a file on disk, usually called something like db.sqlite. The migrate commands is what creates it. You're meant to have a different file on your computer to the one on PythonAnywhere.

But if you accidentally commit db.sqlite into your git repo, then when you clone your code from github onto PythonAnywhere, the db.sqlite file will come down as well, and that would explain why it says "no migrations to apply* - the database is already there, and it's the same as the one on your PC.

Could that be it?

Hi Harry,

Even though I have db.sqlite3 in my .gitignore, now that I check by git repo I see that the db.sqlite3 file is there. So, are you saying that the minimalist djangogirls directions for creating a db will work on pythonanywhere as long as the db.sqlite3 file is not in the git repo? I was worried about later commits overriding all the changes I made to the settings.py file on pythonanywhere.

Okay, I decided to try it. On my local computer, I did:

$ git rm db.sqlite3

Then I did a commit:

$ git commit -m "Delete db.sqlite3"

Then I reran the migrations:

$ python manage.py migrate

Then I pushed to git:

$ git push

And now my git repo does not contain a db.sqlite3 file. Then at pythonanywhere, I did:

$ git pull

Then in the Web tab, I clicked the green reload button, and I tried to visit:

localhost:8000/admin/

but I got the following error:

OperationalError at /admin/
no such table: django_session
Request Method: GET
Request URL:    http://7stud.pythonanywhere.com/admin/
Django Version: 1.9
Exception Type: OperationalError
Exception Value:    
no such table: django_session
Exception Location: /home/7stud/my-first-blog/djgirls/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py in execute, line 323
Python Executable:  /usr/local/bin/uwsgi
Python Version: 3.4.3
Python Path:    
['/var/www',
 '.',
 '',
 '/var/www',
 '/home/7stud/my-first-blog/djgirls/lib/python3.4',
 '/home/7stud/my-first-blog/djgirls/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/7stud/my-first-blog/djgirls/lib/python3.4/lib-dynload',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/7stud/my-first-blog/djgirls/lib/python3.4/site-packages',
 '/home/7stud/my-first-blog']
Server time:    Tue, 29 Dec 2015 12:57:47 +0000

Hi there, that should work as described in the tutorial now, yes. You just need to make sure the database has been deleted on PythonAnywhere with an rm db.sqlite3. And then, after you do the git pull, you have to run the migrate command on PythonAnywhere, just like you did on your own PC

rm db.sqlite3
git pull
python manage.py migrate

Hi Harry,

Thanks for your help. I finally got the simple djangogirls steps to work.

You just need to make sure the database has been deleted on PythonAnywhere with:

rm db.sqlite3.

I find that altering anything at pythonanywhere is problematic: the next time I do git pull, I get merge errors, and to get rid of them I have to issue the following commands in a bash shell on pythonanywhere:

$ git fetch
$ git reset --hard origin/master

As far as I understand it, those commands say, "Retrieve all the files from git, and ignore the files on pythonanywhere.

It's less problematic to remove the files locally:

$ git rm <filename>
$ git commit -m "Delete <filename>"

Then do the push and pull:

$ git push

On pythonanywhere:

$ git pull

By the way, I must have screwed up the order of the djangogirls steps somewhere, but I still think the .gitignore file should be created in the install steps in Chapter 1.

Currently it's in the deploy chapter -- the instructions say to create the gitignore as soon as you install git, before even the first git commit: http://tutorial.djangogirls.org/en/deploy/index.html

That seems like just about the earliest sensible place to put it, to me, but you can always drop the djangogirls organisers a suggestion or a pull request...

There's even a detailed note explaining exactly why we don't want db.sqlite in the repo! I guess maybe you skipped that section because you figured you already had git installed?

Hi Harry,

Yeah, I read the note. I created my .gitignore file earlier in order to ignore vim swap files, and I must not have added the additional entries described in the tutorial until after I did my migrations.

I have had so many problems in the past with git not ignoring files I've listed in .gitignore that I try to create the .gitignore file as the first step for any project I create. It seems that one way or another the .gitignore file always causes me problems.

Thanks for all your help.