need help - following django tutorial to create polls database - missing "on delete cascade" - using django 2.2.1 with mysql 8.0

33 views
Skip to first unread message

K Tan

unread,
May 28, 2019, 8:44:44 PM5/28/19
to django...@googlegroups.com
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?


--

/ per ardua ad astra /

Rudy Quiroga Gamboa

unread,
Jun 27, 2019, 10:16:35 PM6/27/19
to Django users
Hi, I have the same problem, did you find the problem or the solution ?

Jasmine Smith

unread,
Jun 28, 2019, 1:16:57 PM6/28/19
to django...@googlegroups.com
Looking into, will get back to you soon. Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/88b392fe-94b8-4355-9d48-e915d7657b2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe Reitman

unread,
Jun 28, 2019, 6:08:15 PM6/28/19
to Django users
The notes say:
  • The exact output will vary depending on the database you are using. The example above is generated for PostgreSQL.
Are you using the default SQLite?

You could try running migrate and then checking the tables to see if the constraint was added using a DB admin viewer.

For SQLite I use - https://sqlitebrowser.org/
For Postgres  - https://www.pgadmin.org/

sachin thakur

unread,
Jul 5, 2019, 10:08:11 AM7/5/19
to django...@googlegroups.com
I am also facing the same problem i am using PostgreSQL data  base . please do share this issue bug

Thanks and Kind Regards
====================
Sachin Thakur
about.me/sachin.thakur 
PG Student
Dept. of Computer Applications,
Christ ( Deemed to Be university) Main Campus Bangalore, India
UG (DSCASC under Bangalore University ) Bangalore, India.




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages