A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

will my postgresql trigger execute even if I set the value from django model at save?

I have created a trigger on each row INSERT/UPDATE to set the created_at and updated_at respectively.

Trigger:

CREATE OR REPLACE FUNCTION insert_update_function() RETURNS trigger AS $BODY$
BEGIN
  -- CHECKING OPERATION TYPE AND DECIDE COLUMN
  IF TG_OP = 'INSERT' THEN
    NEW.created_at := current_timestamp;
  ELSE
    NEW.updated_at := current_timestamp;
  END IF;
  RETURN NEW;
END; $BODY$ LANGUAGE plpgsql VOLATILE;

Apply Trigger only those table where updated_at column exists

DO $$
DECLARE
    t text;
BEGIN
    FOR t IN
        SELECT table_name FROM information_schema.columns WHERE column_name = 'updated_at'
    LOOP
        EXECUTE format('CREATE TRIGGER insert_update_trigger
                    BEFORE INSERT OR UPDATE ON %I
                    FOR EACH ROW EXECUTE PROCEDURE insert_update_function()', t,t);
    END loop;
END;
$$ language plpgsql;

So when I do INSERT/UPDATE from raw SQL I don't provide these two columns i.e created_at and updated_at, it is automatically trigged and added on my table which is expected and fine but now my query is when I try to INSERT/UPDATE row's of the table through the Django BaseModel where I am setting created_at and updated_at, the trigger will be again called?

Django BaseModel with custom save()

class TimeStampedModel(models.Model):
    """
    An abstract base class model that provides self-updating
    ``created_at`` and ``modified_at`` fields.
    """
    created_at = AutoCreatedField('created_at')
    updated_at = AutoLastModifiedField('updated_at')

    def save(self, *args, **kwargs):
        """
        Overriding the save method in order to make sure that
        updated_at field is updated even if it is not given as
        a parameter to the update field argument.
        """
        update_fields = kwargs.get('update_fields', None)
        if update_fields:
            kwargs['update_fields'] = set(update_fields).union({'updated_at'})

        super().save(*args, **kwargs)

    class Meta:
        abstract = True

Upvotes: 0

Views: 891

Answers (1)

Sahap Asci
Sahap Asci

Reputation: 951

yes, it will be called. I suggest you to create default value for created_at field which will set the value current_timestamp if django does not provide the value of that field.
Here is a sample code;

CREATE OR REPLACE FUNCTION public.insert_update_function() 
RETURNS trigger AS 
$BODY$
BEGIN
  -- CHECKING OPERATION TYPE AND DECIDE COLUMN
  IF TG_OP = 'INSERT' THEN
    -- if created_at comes null from application code then set current timestamp
    IF NEW.created_at IS NULL THEN
      NEW.created_at = current_timestamp;
    END IF;
  ELSE
    -- if updated_at is not provided in application code its value 
    --    will be equal to its old value even its `null`
    --    then we set current timestamp
    --    else nothing to do 
    IF OLD.updated_at IS NOT DISTINCT FROM NEW.updated_at THEN
      NEW.updated_at = current_timestamp;
    END IF;
  END IF;
  RETURN NEW;
END; 
$BODY$ 
LANGUAGE plpgsql 
VOLATILE;



DO LANGUAGE plpgsql
$$
DECLARE
  r record;
BEGIN
  -- set default value for all tables which has created_at column
  FOR r IN
    SELECT table_schema s, table_name t FROM information_schema.columns WHERE column_name = 'created_at'
  LOOP
    -- set default value
    EXECUTE format('ALTER TABLE %I.%I ALTER created_at SET DEFAULT CURRENT_TIMESTAMP', r.s, r.t);
    
    -- fill empty created_at values with current timestamp
    EXECUTE format('UPDATE %I.%I SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL', r.s, r.t);
    
    -- created_at cannot be not null anymore
    EXECUTE format('ALTER TABLE %I.%I ALTER created_at SET NOT NULL', r.s, r.t);
    
  END loop;

  -- add update trigger for updated_at field.
  FOR r IN
    SELECT table_schema s, table_name t FROM information_schema.columns WHERE column_name = 'updated_at'
  LOOP
    EXECUTE format('CREATE TRIGGER %I BEFORE INSERT OR UPDATE ON %I.%I FOR EACH ROW EXECUTE PROCEDURE public.insert_update_function()', 
      r.t || '_insert_update_trigger',r.s,r.t,r.t);
  END loop;
END;
$$;

Upvotes: 1

Related Questions