pgtrigger.Q not referencing the foreign key table properly

Viewed 22

I'm trying to implement a simple pgtrigger.Protect on a table that will do a boolean value check to a foreign key's column. I have written the trigger correctly but when the migration happens, the foreign key's column is not properly referenced. Need help.

User Table

from datetime import datetime as DateTime

from django.db.models import (
    BooleanField, CASCADE, CharField, DateField, DateTimeField, EmailField,
    ForeignKey, IntegerField, JSONField, Model, OneToOneField
)
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
import pgtrigger

from app.models.user.util.manager import UserAccountManager



class User(AbstractBaseUser, PermissionsMixin):

    class Meta:
        db_table = "user"
        managed = True
        triggers = [
            pgtrigger.SoftDelete(name = "User_SOFTDELETE", field = "is_active", value = False)
        ]

    id = CharField(
        max_length = 10,
        primary_key = True,
        null = False,
        blank = False,
        unique = True
    )
    account_name = CharField(max_length=50, null=False, blank=False, unique=True)
    dob = DateField(auto_now=False, auto_now_add=False, null=False, blank=False)
    email = EmailField(max_length=50, null=False, blank=False, unique=True)
    password = CharField(max_length=254, null=False, blank=False)
    content_provider = BooleanField(default=False, null=False, blank=False)

    is_staff = BooleanField(default=False, null=False, blank=False)
    is_active = BooleanField(default=False, null=False, blank=False)

    USERNAME_FIELD = "id"
    REQUIRED_FIELDS = ["account_name", "dob", "email", "password"]

    objects = UserAccountManager()

    def __str__(self):
        return self.account_name



class Product(Model):

    class Meta:
        db_table = "product"
        triggers = [
            pgtrigger.SoftDelete(name = "Product_SOFTDELETE", field = "is_active", value = False),
            pgtrigger.Protect(
                name = "Product_PROTECT_insert",
                operation = pgtrigger.Insert | pgtrigger.Update,
                when = pgtrigger.Before,
                condition = pgtrigger.Q(new__id_provider__content_provider = True)
            )
        ]

    id = CharField(
        max_length = 20,
        primary_key = True,
        null = False,
        blank = False,
        unique = True
    )
    id_provider = ForeignKey(User, db_column="id_provider", on_delete=CASCADE)
  .....
  .....
  .....

Migration Error:

LINE 45: FOR EACH ROW WHEN (NEW."content_provider")

In the migration file, it needs to be converted to something like NEW."user"."content_provider". How can I create this trigger properly to a foreign key's column? Need Help

0 Answers
Related