0

I'm currently working on Django project. Inside it I have an application 'Basketball'. That app have models such as 'Team', 'Player' and others. I've put my project on Heroku.

My problem is that I can access data in 'Basketball' models only by Django ORM. When I use raw SQL database doesn't see my tables.

To show an example, when I use Django shell of my project on Heroku

:~/$ heroku run python3 manage.py shell
>>> from Basketball.models import Player
>>> players = Player.objects.all()

Variable 'players' indeed contains all instances of 'player' model. When I explore database through Heroku command line

:~/$ heroku pg:psql

And when I list all tables

my_project::DATABASE=> \dt

I get following output:

List of relations
 Schema |            Name            | Type  |     Owner      
--------+----------------------------+-------+----------------
 public | Basketball_contract        | table | **************
 public | Basketball_match           | table | **************
 public | Basketball_matchstats      | table | **************
 public | Basketball_player          | table | **************
 public | Basketball_roster          | table | **************
 public | Basketball_team            | table | **************
 public | auth_group                 | table | **************
 public | auth_group_permissions     | table | **************
 public | auth_permission            | table | **************
 public | auth_user                  | table | **************
 public | auth_user_groups           | table | **************
 public | auth_user_user_permissions | table | **************
 public | django_admin_log           | table | **************
 public | django_content_type        | table | **************
 public | django_migrations          | table | **************
 public | django_session             | table | **************
 public | postman_message            | table | **************

But when I try to execute

my_project::DATABASE=> SELECT * FROM Basketball_player;

I get

ERROR:  relation "basketball_player" does not exist
LINE 1: SELECT * FROM Basketball_player;

When I do makemigrations on my project on Heroku

:~/$ heroku run python3 manage.py makemigrations

It makes some migrations

Migrations for 'Basketball':
  0001_initial.py:
    - Create model Contract
    - Create model Match
    - Create model MatchStats
    - Create model Player
    - Create model Roster
    - Create model Team
    - Add field team to roster
    - Add field player to matchstats
    - Add field away to match
    - Add field home to match
    - Add field player_signed to contract
    - Add field team to contract

But when I try to apply them

:~/$ heroku run python3 manage.py migrate

That messages shows

Operations to perform:
  Synchronize unmigrated apps: staticfiles, mathfilters, django_countries, messages
  Apply all migrations: auth, sessions, admin, contenttypes, postman
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
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.
4
  • You must not run makemigrations on Heroku. Do it locally, commit to git, and push to Heroku, then run the migrations. Commented Dec 19, 2015 at 10:15
  • @DanielRoseman thank you for your comment. But don't makemigrations command applies changes in database? I mean that I have different DB locally and different on Heroku. Commented Dec 19, 2015 at 13:30
  • No. makemigrations creates migration files on disk to be run with migrate. But on Heroku the disk is ephemeral, files do not persist between runs. As I said, you must create the migration files locally and upload to Heroku before running migrate. Commented Dec 19, 2015 at 14:51
  • Naturally, you are right. I was writing about makemigrations while was thinking about migrate. Thanks! Commented Dec 19, 2015 at 15:15

1 Answer 1

2

in PostgreSQL when you are using uppercase you have to embrace them with quotation marks so it should be

    SELECT * FROM "Basketball_player"

instead of

    SELECT * FROM Basketball_player
Sign up to request clarification or add additional context in comments.

3 Comments

Quite an easy solution for such a long question :) Thanks! Still wondering about migrating, but I believe it's a different question.
if it is working you can accept the question
Yes, I just checked. Thank you for your help. Also makemigrations issue was explained in comments under my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.