Just an update here: IF (TG_OP = 'INSERT') THEN > UPDATE public.companies SET client_code_increment = > (client_code_increment + 1) WHERE id = NEW.company_id;
The line above was updating the client_code_increment even if the customer was inserting data by hiimself, which is wrong. The client_code_increment must be updated IF is an insert AND if the customer did not insert data into the code column. *Correction:* IF (TG_OP = 'INSERT') AND NEW.code IS NULL THEN > UPDATE public.companies SET client_code_increment = > (client_code_increment + 1) WHERE id = NEW.company_id; Cheers Lucas