6

I'm on part 2 of the Django tutorial. This is the error I get when try to add a "choice" in Django administration

DatabaseError: table polls_choice has no column named poll_id

This is what I get when I run the command

python manage.py sql polls

BEGIN;
CREATE TABLE "polls_poll" (
    "id" integer NOT NULL PRIMARY KEY,
    "question" varchar(200) NOT NULL,
    "pub_date" datetime NOT NULL
)
;
CREATE TABLE "polls_choice" (
    "id" integer NOT NULL PRIMARY KEY,
    "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id"),
    "choice_text" varchar(200) NOT NULL,
    "votes" integer NOT NULL
)
;

COMMIT; 

This is my settings.py

http://pastebin.com/g4KvigqX

Any help is much appreciated! thank you!

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
Liondancer
  • 15,721
  • 51
  • 149
  • 255

2 Answers2

11

It sounds like you added the poll foreign key after you ran syncdb. The syncdb creates the table, but does not do migrations if you add/change/remove fields.

For Django 1.7+, you should use migrations instead of syncdb. For Django < 1.7, south is highly recommended for doing database migrations.

However, since you are working through the tutorial, and don't have any important data, the easiest thing to do is drop the table and recreate it. Run the following command in a database shell:

drop table polls_choice;

Then run syncdb again to recreate the table.

If you don't need any data in your db, it would be even quicker to delete the sqlite db file, then run syncdb again.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Not sure how to delete polls_choice on sqlite3 =/ Can't seem to display my tables in the shell – Liondancer Jul 29 '13 at 11:27
  • 2
    @Liondancer You can't access an sqlite database through the command line. Your best bet is to delete the database file, recreate it, and run syncdb again. –  Jul 29 '13 at 11:33
  • 1
    @Ergusto Deleting the db file and running syncdb again is probably the quickest thing to do if there's no important data. But what do you mean by *you can't access an sqlite database through the command line*? What about the [dbshell](https://docs.djangoproject.com/en/1.5/ref/django-admin/) management command, or `sqlite3 `? – Alasdair Jul 29 '13 at 11:37
  • 1
    @Alasdair It seems you are correct. I'll edit my response. Edit: Can't edit it for some reason. That's actually a little frustrating though. I've been in that situation before and always just resorted to deleting the file. –  Jul 29 '13 at 11:40
  • @Alasdair I'm pretty new to sqlite3. I tried the command from http://stackoverflow.com/questions/82875/how-do-i-list-the-tables-in-a-sqlite-database-file but it nothing appeared =/ – Liondancer Jul 29 '13 at 11:46
  • 3
    Running `python ./manage dbshell` then `.tables` should work. – Alasdair Jul 29 '13 at 11:53
5

Did you run python manage.py syncdb before creating the foreign key on the Choice to Poll? You may need to delete the database and run it again.

meshy
  • 8,470
  • 9
  • 51
  • 73