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

So, I have the following problem:

I have a Django class, Article, which contains several Char- and TextFields. They were set to blank=True AND null=True which is... unfortunate. Oh well, no I need to fix that. So after I deleted null=True and set default='', I wrote the following thing into my migration:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import languagefields.utils
from languagefields.utils import LANGUAGES
from django.utils.translation import activate
from news.models import Article


def null_migrations(apps, schema_editor):
    activate('en')
    fields = ['short_title', 'description']
    for p in Article.objects.all():
        for l in LANGUAGES:
            for f in fields:
                if p.get_language(l, f) is None:
                    p.set_localized(l, f, '')
                    p.save()


class Migration(migrations.Migration):

    dependencies = [
        ('news', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(null_migrations),
        migrations.AlterField(....

The fields are custom fields, based on the default Char-/TextFields, which enable translation. So there is a bunch of them. For every field you create there will be 5, a description in english, german and so on... So yeah, the little function works fine, I ran it on the server and cleaned the db entries manually, but that doesn't stop the exception above. So I thought I'd put it in the migration to clean out on the go. But still the exception. What am I doing wrong?

Thank you in advance :)

EDIT

Split up the migrations in two:

0002_null_cleaning.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import languagefields.utils
from languagefields.utils import LANGUAGES
from django.utils.translation import activate
from news.models import Article


def null_migrations(apps, schema_editor):
    activate('en')
    fields = ['short_title', 'description']
    for p in Article.objects.all():
        for l in LANGUAGES:
            for f in fields:
                if p.get_language(l, f) is None:
                    p.set_localized(l, f, '')
                    p.save()


class Migration(migrations.Migration):

    dependencies = [
        ('news', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(null_migrations),
]

0003_data_migration

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import languagefields.utils


class Migration(migrations.Migration):

    dependencies = [
        ('news', '0002_null_cleaning'),
    ]

    operations = [
        migrations.AlterField(...

Still the same error, happening for 0003

Ozymandias
  • 199
  • 6
  • 17
  • Possible duplicate of [Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events](https://stackoverflow.com/questions/12838111/django-db-migrations-cannot-alter-table-because-it-has-pending-trigger-events) – maazza May 17 '19 at 11:01

1 Answers1

0

I think you have to split the migration into 2 actual migrations. They should happen in a transaction, so altering the data and altering the table is probably not kosher. Just create two migrations and it should work.

reptilicus
  • 10,290
  • 6
  • 55
  • 79
  • I tried to do that, split it up. I edited my post to show how I did it. It didn't change anything, do you know if I am doing something wrong there? – Ozymandias Aug 23 '17 at 15:38