DROP FUNCTION IF EXISTS vclib.t_entities_attributes_insert CASCADE;
CREATE  FUNCTION vclib.t_entities_attributes_insert()
RETURNS TRIGGER AS $$
BEGIN
    INSERT INTO ies.t_entity_attributes_change(
        entityattributeschangeid, entityid, "index", attributetype, "name", description, systemtype, maxlength, "precision", "scale", defaultformat, iscalculated, isflag, isrequired,
        mappingtype, mappingname, save, linkattributename, choiceattributedata, isstored, attrlexem, pathsattributes, iscashed, isadditional, mappingtable, mappingreftable, parametersdata, customsdata, requiredattributes,
        sortingattribute, enumtype, isidentity, iscustom, removeaction, isprimarykey, entitiesattributesid, editdate, "action"
    )
    SELECT
        gen_random_uuid(), NEW.entityid, NEW.index, NEW.attributetype, NEW.name, NEW.description, NEW.systemtype, NEW.maxlength, NEW.precision, NEW.scale, NEW.defaultformat, NEW.iscalculated, NEW.isflag, NEW.isrequired,
        NEW.mappingtype, NEW.mappingname, NEW.save, NEW.linkattributename, NEW.choiceattributedata, NEW.isstored, t2.attrlexem, t2.pathsattributes, NEW.iscashed, NEW.isadditional, NEW.mappingtable, NEW.mappingreftable, NEW.parametersdata, NEW.customsdata, NEW.requiredattributes,
        NEW.sortingattribute, NEW.enumtype, NEW.isidentity, NEW.iscustom, NEW.removeaction, NEW.isprimarykey, NEW.entitiesattributesid, NOW(), 0
    FROM vclib.t_entities_attributes t2 where  t2.entitiesattributesid = NEW.entitiesattributesid;
    RETURN OLD; 
END;
$$ LANGUAGE plpgsql;


DROP TRIGGER IF EXISTS t_entities_attributes_after_insert ON vclib.t_entities_attributes;
CREATE TRIGGER t_entities_attributes_after_insert
AFTER INSERT ON vclib.t_entities_attributes
FOR EACH ROW EXECUTE FUNCTION vclib.t_entities_attributes_insert();