The notes say:
On Tuesday, May 28, 2019 at 7:44:44 PM UTC-5, K Tan wrote:
-- - The exact output will vary depending on the database you are using. The example above is generated for PostgreSQL.
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/
On Tuesday, May 28, 2019 at 7:44:44 PM UTC-5, K Tan wrote:
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 choiceNow when I run this command:<quote>LITTLEBLACK:www samktan$ python3 manage.py sqlmigrate polls 0001BEGIN;
--
-- 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 /
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/362bb406-8d44-4285-8ac5-7ae3af832d7a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment