Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.db.utils.OperationalError: cannot ALTER TABLE "forum_thread" because it has pending trigger events

I am trying to change accepted_answer ForeignKey to a BooleanField and while migrating getting the error django.db.utils.OperationalError: cannot ALTER TABLE "forum_thread" because it has pending trigger events. This is the models.py of before:

class Thread(models.Model):
    title = models.CharField(max_length=300)
    answer_count = models.PositiveIntegerField(default=0)
    added_at = models.DateTimeField(auto_now_add=True)
    accepted_answer = models.ForeignKey('forum.Post', null=True, blank=True, related_name='+')
like image 703
Ranvijay Sachan Avatar asked Mar 15 '17 11:03

Ranvijay Sachan


1 Answers

This error seems to happen when trying to update and change schema of the same table in one operation.

For example, after providing new default values when prompted by makemigrations when removing null=True from a field, this error seems to appear because Django is trying to change nullability while also updating the column.

Workarounds, depending on situation:

  • Easiest is often adding a new field with the desired properties, then removing the old field (maybe after copying data over).
  • If the error can be avoided by setting values for all fields that were needing a new default value (Django asking during the migration), try setting those values before making the migration/schema change.

In your specific case, adding a new field then removing the old is probably the way to go.

like image 82
Max Lemieux Avatar answered Nov 05 '22 10:11

Max Lemieux