               'WHERE i.record_id = ' || quote_literal(ref_field_id) || 
               '  AND people_ref = ' || quote_literal(OLD.people_ref) || 
               '  AND i.referenced_relation = ' || quote_literal(ref_relation) INTO rec_exists ;
-      /* 'IF rec_exists then that people exist in an other identification of the same record, so we must not remove it from flat' */
       IF rec_exists IS NOT NULL THEN
         RETURN OLD;
       END IF ;

CREATE OR REPLACE FUNCTION fct_filter_encodable_row(ids varchar, col_name varchar, user_id integer) RETURNS SETOF integer
AS $$
DECLARE
  ref refcursor;
  rec RECORD;
BEGIN
        OPEN ref FOR EXECUTE 'SELECT distinct(' || quote_ident(col_name) || ') as result ' ||
          ' FROM darwin_flat ' ||
          'WHERE '|| quote_ident(col_name) || ' in (select X::int from regexp_split_to_table(' || quote_literal($1) || ', '','' ) as X) ' ||
          'AND collection_ref in (select X FROM fct_search_authorized_encoding_collections(' || user_id || ') as X)';

        LOOP
        FETCH ref INTO rec;
            IF  NOT FOUND THEN
                EXIT;  -- exit loop
            END IF;

        return next rec.result;

        END LOOP;

        CLOSE ref;

END;
$$ LANGUAGE plpgsql;

            '              WHERE ' || quote_ident(field_to_update) || ' && ARRAY[' || NEW.people_ref::integer || '] ' || 
            '                AND ' || quote_ident(ref_field) || ' = ' || quote_literal(ref_field_id) ||
            '             )' INTO rec_exists ;
    IF rec_exists = TRUE THEN
      RETURN NEW ;
    END IF;
