Hi, everyone,
This is my first time using Django and I think I'm missing something or there is a bug. I am following the instructions on (https://docs.djangoproject.com/en/2.2/intro/tutorial02/) and I've just added the following chunk of code to "polls/models.py". (I copied/pasted so I know it's correct.)
<quote>
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
</quote>
Then I ran the following command:
<quote>
LITTLEBLACK:www samktan$ python3 manage.py makemigrations polls
Migrations for 'polls':
polls/migrations/0001_initial.py
- Create model Question
- Create model Choice
</quote>
Which is missing one line compared to the tutorial:
- Add field question to choice
Now when I run this command:
<quote>
LITTLEBLACK:www samktan$ python3 manage.py sqlmigrate polls 0001
BEGIN;
--
-- Create model Question
--
CREATE
TABLE `polls_question` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY
KEY, `question_text` varchar(200) NOT NULL, `pub_date` datetime(6) NOT
NULL);
--
-- Create model Choice
--
CREATE TABLE
`polls_choice` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`choice_text` varchar(200) NOT NULL, `votes` integer NOT NULL,
`question_id` integer NOT NULL);
ALTER TABLE `polls_choice` ADD CONSTRAINT `polls_choice_question_id_c5b4b260_fk_polls_question_id` FOREIGN KEY (`question_id`) REFERENCES `polls_question` (`id`);
COMMIT;
</quote>
It is missing the "on delete cascade" clause, which I suspect it caused by the missing line above.
I have confirmed in MySQL that the "on delete cascade" clause is definitely missing.
<quote>
mysql> show create table `polls_choice`;
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
Table | Create Table
|
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| polls_choice | CREATE TABLE `polls_choice` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`choice_text` varchar(200) COLLATE utf8mb4_general_ci NOT NULL,
`votes` int(11) NOT NULL,
`question_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `polls_choice_question_id_c5b4b260_fk_polls_question_id` (`question_id`),
CONSTRAINT `polls_choice_question_id_c5b4b260_fk_polls_question_id` FOREIGN KEY (`question_id`) REFERENCES `polls_question` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci |
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
</quote>
Can someone tell me what I'm doing wrong?
--