1

I have the same problem described here I am working with QGIS 2.18.10 and postgis 9.4.

Is there a way to make QGIS able to split the line and insert in the constrained field the expression "nextval('fogn_tronchi_cod_tronco_seq'::regclass)"?

My table is like this:

     gid        | integer                | non null preimpostato nextval('fogn_tronchi_gid_seq'::regclass)
 anno       | numeric(10,0)          |
 codice_via | numeric(10,0)          |
 diametro   | numeric(10,0)          |
 id_n1      | character varying(255) |
 id_n2      | character varying(255) |
 materiale  | character varying(255) |
 nome_via   | character varying(255) |
 note       | character varying(255) |
 pendenza   | numeric                |
 pend_ind   | numeric                |
 proprieta  | character varying(255) |
 qfondo_mon | numeric                |
 qfondo_val | numeric                |
 regime     | character varying(255) |
 stato_cons | character varying(255) |
 tipologia  | character varying(255) |
 sollev_rec | character varying(255) |
 recett_rec | character varying(255) |
 comune     | character varying(255) |
 collaudo   | character varying(255) |
 accesso    | character varying(255) |
 prop_serv  | character varying(255) |
 gestito    | character varying(4)   |
 criticita  | character varying(255) |
 the_geom   | geometry               |
 cod_tronco | integer                | non null preimpostato nextval('fogn_tronchi_cod_tronco_seq'::regclass)
Indici:
    "fogn_tronchi_pkey" PRIMARY KEY, btree (gid)
    "fogn_tronchi_cod_tronco_key" UNIQUE CONSTRAINT, btree (cod_tronco)
    "fogn_tronchi_the_geom_gist" gist (the_geom)
Vincoli di controllo:
    "enforce_dims_the_geom" CHECK (st_ndims(the_geom) = 2)
    "enforce_geotype_the_geom" CHECK (geometrytype(the_geom) = 'MULTILINESTRING'::text OR the_geom IS NULL)
    "enforce_srid_the_geom" CHECK (st_srid(the_geom) = 3003)

1 Answer 1

1

You can have a trigger that sets the value. As the trigger occurs before the insert operation, it also occurs before the constraints are checked.

Since the trigger sets the value, you would need to remove the default value from the table definition, else the ID will jump by 2 between each record.

Note that the value will be set when you save your edits, so you may have temporary blank, duplicates etc.

 CREATE OR REPLACE FUNCTION fogn_tronchi_set_uid()
 RETURNS "trigger" AS
 $BODY$
 BEGIN
   New.gid:=nextval('fogn_tronchi_cod_tronco_seq');
   Return NEW;
 END;
 $BODY$
 LANGUAGE 'plpgsql' VOLATILE;

 CREATE TRIGGER fogn_tronchi_set_uid_tg
 BEFORE INSERT
 ON fogn_tronchi
 FOR EACH ROW
 EXECUTE PROCEDURE fogn_tronchi_set_uid();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.