Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30748 closed Bug (worksforme)

Abstract model class specifies foreign key – MySQL doesn't have it.

Reported by: Mike Robinson Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
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 Mike Robinson)

The abstract class is:

class AbstractForumProfile(models.Model):
    """
    Represents the profile associated with each forum user.
    """
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='forum_profile',
        verbose_name=_('User'))

    # The user's avatar.
    avatar = ExtendedImageField(
        verbose_name=_('Avatar'), null=True, blank=True,
        upload_to=machina_settings.PROFILE_AVATAR_UPLOAD_TO,
        **machina_settings.DEFAULT_AVATAR_SETTINGS)

    # The user's signature.
    signature = MarkupTextField(
        verbose_name=_('Signature'), blank=True, null=True,
        validators=[validators.NullableMaxLengthValidator(
            machina_settings.PROFILE_SIGNATURE_MAX_LENGTH)])

    # The amount of posts the user has posted (only approved posts are considered here).
    posts_count = models.PositiveIntegerField(verbose_name=_('Total posts'), blank=True, default=0)

The derived class is:

class ForumProfile(AbstractForumProfile):
    auto_subscribe_topics = models.BooleanField(
        verbose_name='Automatically subscribe to topics you create.',
        default=False)
                
    notify_subscribed_topics = models.BooleanField(
        verbose_name='Receive an email notification on new replies to subscribed topics.',
        default=False)

but the resulting MySQL syntax (which does not show ON DELETE CASCADE on the FOREIGN KEY constraint) is:

CREATE TABLE `forum_member_forumprofile` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `avatar` varchar(100) DEFAULT NULL,
  `signature` longtext,
  `posts_count` int(10) unsigned NOT NULL,
  `_signature_rendered` longtext,
  `user_id` int(11) NOT NULL,
  `auto_subscribe_topics` tinyint(1) NOT NULL DEFAULT '0',
  `notify_subscribed_topics` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`),
  CONSTRAINT `forum_member_forumprofile_user_id_9d6b9b6b_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=latin1;

Change History (7)

comment:1 by Mike Robinson, 5 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 5 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: invalid
Status: newclosed
Summary: Abstract model class specifies foreign key – MySQL doesn't have itAbstract model class specifies foreign key – MySQL doesn't have it.
Type: UncategorizedBug
Version: 2.2master

Django doesn't use ON DELETE CASCADE it emulates its behavior (see documentation).

comment:3 by Mike Robinson, 5 years ago

Reopening this, because something is broken:

django.db.utils.IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (`gga$GGA`.`forum_member_forumprofile`, CONSTRAINT `forum_member_forumprofile_user_id_9d6b9b6b_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')

If Django was supposed to do the cascading deletes by its own logic, it didn't.

comment:4 by Mike Robinson, 5 years ago

Resolution: invalid
Status: closednew

comment:5 by Mariusz Felisiak, 5 years ago

Resolution: worksforme
Status: newclosed

I cannot reproduce this issue with provided models, everything works properly for me. You could provided a sample project but due to the fact that this behavior is tested I don't think that there it is an issue in Django. I would rather use one of support channels.

comment:6 by Mike Robinson, 5 years ago

I will attempt to produce the simplest possible test case and report back. However, this issue occurs within the Django Admin when attempting to delete a User. The table n question is the user-profile as shown above.

But also: I think that I am seeing other instances of this in the same application – other cases involving foreign keys and deletion. (Delete posts, etc.) Yes, it is possible that this is some obscure application (model) issue and I will indeed pursue this to try to help resolve the problem. But I am suspicious, specifically, of some issue in OneToOneField.

Version 0, edited 5 years ago by Mike Robinson (next)

comment:7 by Mike Robinson, 5 years ago

Changed status to "worksforme" since there *is* something wrong here but "it's not going to be quite that easy, as it turns out" to reproduce it. So it goes.

Note: See TracTickets for help on using tickets.
Back to Top