Leeliam199
Leeliam199

Reputation: 9

Database Api Python/django

I am working on a tutorial on Django

When I invoke the interactive shell to access the database API using the command python manage.py shell, I receive the following prompt

In [1]:from music.models import Album, Song
In [2]: Album.objects.all()

Then the next line gives me this

OperationalError: no such table: music_album

Upvotes: 0

Views: 102

Answers (2)

juankysmith
juankysmith

Reputation: 12448

Configure your database connection in your settings.py and, after creating Django models, you have to execute two commands:

  • python manage.py makemigrations
  • python manage.py migrate

Then tables will be created in the database and you will be able to use then in the shell

Upvotes: 3

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476699

Like the error says, it means that your database has no tables that correspond to these models.

Typically that means that you forgot to migrate your database. By running:

python manage.py makemigrations

Django will construct files (in someapp/migrations/) these files describe the steps that have to be carried out such that tables are constructed with the correct columns, or later in the progress columns can be renamed, tables removed, new tables constructed. If you run the command, you will see new files popping up that describe these migrations.

But now you still did not migrate the database itself, you only constructed the files. It is for example possible that you want to add custom migrations yourself. In case the migration files are correct, you can run

python manage.py migrate

to change the database accordingly.

In order to run the migrations, the database has to be specified correctly in the settings.py file. But since you obtain an OperationalError that complains about the table not being found, I think this has already been handled (correctly).

See the Django topic on migrations [Django-doc] for more information.

Upvotes: 3

Related Questions