How would you ignore a trigger within another trigger with django-pgtrigger

I am updating tbla."field_2" from a trigger in tblb. Except tbla."field_2" is protected with a pgtrigger.ReadOnly trigger.

This works without the pgtrigger.ReadOnly trigger in TblA. How would I do it and keep the pgtrigger.ReadOnly trigger on TblA.field_2?

Model TblA(models.Model):
    field_1 = models.CharField(max_length=20)
    field_2 = models.PositiveIntegerField(default=0)

    class Meta:
        triggers = [
            pgtrigger.ReadOnly(
                name='read_only_field_2',
                fields=['field_2']
            )
        ]
class TblB(models.Model):
    field_a = models.ForeignKey(TblA, on_delete=models.SET_NULL)

    class Meta:
        pgtrigger.Trigger(
                func=pgtrigger.Func('''
                    IF (NEW."{columns[field_a]}" IS NOT NULL) THEN
                        UPDATE "tbla"
                            SET "tbla"."field_2" = "tbla"."field_2" + 1
                        WHERE
                            "tbla"."id" = NEW.{columns[field_a]}
                    END IF;
                    RETURN NEW;
                    ''')
                ),
                when=pgtrigger.Before,
                operation=pgtrigger.Insert,
                name='increment_tbla_field_2'
            ),

I was thinking to mimic pgtrigger.ignore() functionality within the trigger by using set_config however I am not very familiar how persistent set_config is. Do I need to clean up at the end of the trigger? Is this doable?

set_config('pgtrigger.ignore', '{myapp:pgtrigger_read_only_field_2_c8215,pgtrigger_read_only_field_2_c8215}', true)
Вернуться на верх