django-pghistory icon indicating copy to clipboard operation
django-pghistory copied to clipboard

django.db.utils.NotSupportedError: cannot alter type of a column used in a trigger definition

Open pierreben opened this issue 2 years ago • 1 comments

Hi,

When creating a new django migration on a model registered with pghistory, I get this error: django.db.utils.NotSupportedError: cannot alter type of a column used in a trigger definition

Here's the full error stack:

poetry run python ./src/backend/manage.py migrate 
Operations to perform:
  Apply all migrations: admin, auth, contact, contenttypes, easy_thumbnails, energy, heritage, history, invoicing, my-user_geo, knox, markets, notification, parameters, pghistory, sessions, sites, weather
Running migrations:
  Applying heritage.0051_auto_20220701_1510...Traceback (most recent call last):
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/pgconnection/core.py", line 85, in execute
    return super().execute(sql, args)
psycopg2.errors.FeatureNotSupported: cannot alter type of a column used in a trigger definition
DETAIL:  trigger pgtrigger_building_snapshot_update_162c2 on table heritage_building depends on column "building_owner"


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/my-user/workspace/my-project/./src/backend/manage.py", line 58, in <module>
    execute_from_command_line(sys.argv)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/migrations/migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/migrations/operations/fields.py", line 244, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 608, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/postgresql/schema.py", line 196, in _alter_field
    super()._alter_field(
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 765, in _alter_field
    self.execute(
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 145, in execute
    cursor.execute(sql, params)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/my-user/workspace/my-project/.venv/lib/python3.10/site-packages/pgconnection/core.py", line 85, in execute
    return super().execute(sql, args)
django.db.utils.NotSupportedError: cannot alter type of a column used in a trigger definition
DETAIL:  trigger pgtrigger_building_snapshot_update_162c2 on table heritage_building depends on column "building_owner"
make: *** [Makefile:68: command] Error 1

I think it can be reproduced using the following steps :

  • Create a model and its migrations

Exemple

class Building(models.Model):
    building_owner = models.CharField(
        _("Owner"),
        max_length=300,
        blank=True,
    )
  • Register it with pghistory and create the migration:
@pghistory.track(pghistory.Snapshot("building.snapshot"))
class Building(models.Model):
    building_owner = models.CharField(
        _("Owner"),
        max_length=300,
        blank=True,
    )
  • Change the size value of a field, create a new migration:
@pghistory.track(pghistory.Snapshot("building.snapshot"))
class Building(models.Model):
    building_owner = models.CharField(
        _("Owner"),
        max_length=350,
        blank=True,
    )
  • When applying the migration I get the error:
django.db.utils.NotSupportedError: cannot alter type of a column used in a trigger definition
DETAIL:  trigger pgtrigger_building_snapshot_update_162c2 on table heritage_building depends on column "building_owner"

If I manually remove the trigger on the db and run the migration, it works and recreate the trigger.

Did I missed something ? Do you have an idea on how we can fix this issue ?

pierreben avatar Jul 01 '22 13:07 pierreben

Hi,

similar thing happens to me when modifying a DecimalField.max_digits. The easiest way I found to deal with this is to add a RunPython operation in the migration file, before the other operations:

[...]

def uninstall_triggers(apps, schema_editor):
    pgtrigger.uninstall(
        "xxx ", "yyy",  # some trigger that you would see when running ./manage.py pgtrigger ls
    )


class Migration(migrations.Migration):

   [...]

    operations = [
        migrations.RunPython(uninstall_triggers),
        [...]

souliane avatar Jul 21 '22 09:07 souliane

Thanks for reporting! This was a bug in django-pgtrigger and was recently addressed in this issue - https://github.com/Opus10/django-pgtrigger/issues/48

In order to get this bug fix, you must upgrad to at least pghistory 2, which pulls in the latest django-pgtrigger. These new releases are totally integrated with the migration system. See #43 and be sure you have django-pgtrigger>=4.5 installed

wesleykendall avatar Sep 06 '22 01:09 wesleykendall