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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With