Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30524 closed Bug (invalid)

on_delete doesn't work properly with MySql.

Reported by: Dheeraj Yadav Owned by: nobody
Component: Uncategorized Version: 2.2
Severity: Normal Keywords: Foreign Key
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Dheeraj Yadav)

Model:

class Attempt(models.Model):
    student = models.ForeignKey(Student, models.CASCADE, related_name='quiz_attempts',null=False)
    quiz = models.ForeignKey(Quiz,on_delete=models.CASCADE,related_name='quiz_attempts')
    score = models.FloatField()
    over = models.BooleanField(default=False)
    date = models.DateTimeField(auto_now_add=True)
    currquestion = models.ForeignKey(Question,null=True,default=None,on_delete=models.SET_NULL)
    
    class Meta:
        indexes = [
            models.Index(fields=['student']),
            models.Index(fields=['quiz']),
        ]

Table created in MySql:

CREATE TABLE `classroom_attempt` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `score` double NOT NULL,
  `over` tinyint(1) NOT NULL,
  `date` datetime(6) NOT NULL,
  `currquestion_id` int(11) DEFAULT NULL,
  `quiz_id` int(11) NOT NULL,
  `student_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `classroom_a_student_1a21bc_idx` (`student_id`),
  KEY `classroom_a_quiz_id_eef64a_idx` (`quiz_id`),
  KEY `classroom_attempt_currquestion_id_545301ef_fk_classroom` (`currquestion_id`),
  CONSTRAINT `classroom_attempt_currquestion_id_545301ef_fk_classroom` FOREIGN KEY (`currquestion_id`) REFERENCES `classroom_question` (`id`),
  CONSTRAINT `classroom_attempt_quiz_id_e227b203_fk_classroom_quiz_id` FOREIGN KEY (`quiz_id`) REFERENCES `classroom_quiz` (`id`),
  CONSTRAINT `classroom_attempt_student_id_a4dc81cd_fk_classroom` FOREIGN KEY (`student_id`) REFERENCES `classroom_student` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------------------+---------------------

As you can notice models.CASCADE is not applied.

Change History (2)

comment:1 by Dheeraj Yadav, 5 years ago

Description: modified (diff)
Keywords: indexes removed

comment:2 by Simon Charette, 5 years ago

Resolution: invalid
Status: newclosed

Django doesn't support database level foreign keys on delete yet, this is tracked in #21961.

The models.CASCADE value implements cascade deletion at the application/Python level in order to support pre_delete and post_delete signals.

Last edited 5 years ago by Simon Charette (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top