<div dir="ltr">I think it's not difficult to use rules. The good thing is having such a simple mechanism inside basic sql, without having to use a procedural language to write functions and triggers to manage views. Although rules are NOT standard SQL and sometimes, when you discard something for convenience, you'll miss it in the future. So someone says that rules are evil... I love them and they are a basic building block for PostgreSQL.<br><div>I assume that you have to insert into the view.</div><div>In this case the rule is fairly simple. To write the statement you have to understand how the sequence works: if you have a NULL in the insert (I mean during the insert into the table, not into the view) the sequence will be activated and the next number in the sequence will be used instead of NULL. So you have two possibilities for your rule:</div><div>1. Remove the ID from your column list (just list all the column besides the ID);</div><div>2. Replicate the same action done when inserting in the base table;</div><div>and I found a third here</div><div><a href="http://stackoverflow.com/questions/12836043/inserting-a-record-into-a-table-with-a-column-declared-with-the-serial-function">http://stackoverflow.com/questions/12836043/inserting-a-record-into-a-table-with-a-column-declared-with-the-serial-function</a><br></div><div>using DEFAULT instead of the field value.</div><div><br></div><div>So the rule will be something like this (I usually make mistakes the first time, sorry in advance...):</div><div><div><div>CREATE RULE v_repository_point_ins AS </div><div>ON INSERT TO schemaapp.v_repository_point</div><div>DO INSTEAD </div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span>INSERT INTO schemaapp.repository (uuid, progetto, procedimento, <b>geometria</b>)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span>VALUES (new.uuid, new.progetto, new.procedimento, new.<b>geometria1</b>)</div><div>;</div></div></div><div>if you choose to let the base table do its work ;.).  Or</div><div><div><div><div>CREATE RULE v_repository_point_ins AS </div><div>ON INSERT TO schemaapp.v_repository_point</div><div>DO INSTEAD </div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>INSERT INTO schemaapp.repository (id, uuid, progetto, procedimento, geometria)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>VALUES (nextval('repository_id_seq'), new.uuid, new.progetto, new.procedimento, new.geometria1)<br></div><div>;</div></div><div>assuming the serial macro created a sequence with that name. Or</div><div>CREATE RULE v_repository_point_ins AS </div><div>ON INSERT TO schemaapp.v_repository_point</div><div>DO INSTEAD </div><div><span class="gmail-Apple-tab-span" style="white-space:pre">   </span>INSERT INTO schemaapp.repository (id, uuid, progetto, procedimento, geometria)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>VALUES (DEFAULT, new.uuid, new.progetto, new.procedimento, new.geometria1)</div><div>;</div></div><div>if you choose the third mechanism.</div></div><div><br></div><div>I would use the first rule for three good reasons:</div><div>1. less elements in your statement means less to mistake for</div><div>2. do not force a thing that is already supposed to work</div><div>3. I trust the light into the fridge to switch off when I close the door.</div><div>If it does not work, better stop and think what's wrong in the database instead of taking an hammer and get it done forcefully.</div><div>c</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 16, 2016 at 8:36 PM, Andrea Peri <span dir="ltr"><<a href="mailto:aperi2007@gmail.com" target="_blank">aperi2007@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><p dir="ltr">Thx for clarification.<br>
My postgres is a 9.5 version. This explain why it has writable view.</p>
<p dir="ltr">So i Need to try the role Instead of sinpky and often used serial type for pk.</p><span class="gmail-HOEnZb"><font color="#888888">
<p dir="ltr">A.</p></font></span><div class="gmail-HOEnZb"><div class="gmail-h5">
<div class="gmail_extra"><br><div class="gmail_quote">Il 16 nov 2016 19:22, "Nyall Dawson" <<a href="mailto:nyall.dawson@gmail.com" target="_blank">nyall.dawson@gmail.com</a>> ha scritto:<br type="attribution"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><p dir="ltr"></p>
<p dir="ltr">On 17 Nov 2016 12:54 AM, "Carlo A. Bertelli (Charta s.r.l.)" <<a href="mailto:carlo.bertelli@gmail.com" target="_blank">carlo.bertelli@gmail.com</a>> wrote:<br>
><br>
> Ciao Andrea,<br>
> by default Postgresql views are read only. Even the simplest view made in Postgresql needs a rule to be updated.</p>
<p dir="ltr">That's not quite correct. Recent versions of postgres make simple views editable.<br>
See</p>
<p dir="ltr"><a href="https://www.depesz.com/2012/12/11/waiting-for-9-3-support-automatically-updatable-views/" target="_blank">https://www.depesz.com/2012/12<wbr>/11/waiting-for-9-3-support-au<wbr>tomatically-updatable-views/</a></p>
<p dir="ltr">Nyall<br><br></p>
<p dir="ltr"> After writing a statement for a view, just take the habit of writing the rules for insert, update, delete.<br>
> Please read the chapter on the Rule System in the Postgresql Reference Manual for the instructions.<br>
> c<br>
><br>
> Message: 6<br>
> Date: Wed, 16 Nov 2016 09:39:23 +0100<br>
> From: Andrea Peri <<a href="mailto:aperi2007@gmail.com" target="_blank">aperi2007@gmail.com</a>><br>
> To: Matthias Kuhn <<a href="mailto:matthias@opengis.ch" target="_blank">matthias@opengis.ch</a>><br>
> Cc: qgis-user <<a href="mailto:qgis-user@lists.osgeo.org" target="_blank">qgis-user@lists.osgeo.org</a>><br>
> Subject: Re: [Qgis-user] Editing a Postgis view<br>
> Message-ID:<br>
>         <CABqTJk8Bo7fs6qOG=MhO9N50qO5L<wbr>+JkXAOAgik=<a href="mailto:tUGWwcKd-WA@mail.gmail.com" target="_blank">tUGWwcKd-WA@mail.<wbr>gmail.com</a>><br>
> Content-Type: text/plain; charset=UTF-8<br>
><br>
> Hi Matthias,<br>
> thx for response.<br>
> So the trouble is in my db.<br>
><br>
> Now I try to understand better why it is not working.<br>
><br>
> This is my actual try test:<br>
><br>
> create table schemaapp.repository(<br>
>  ID serial primary key NOT NULL,<br>
>  UUID varchar(50) NOT NULL,<br>
>  PROGETTO varchar(9) NOT NULL,<br>
>  PROCEDIMENTO text NOT NULL,<br>
>  GEOMETRIA1 geometry(MULTIPOINT, 3003)<br>
> );<br>
><br>
> create view schemaapp.v_repository_point as<br>
>  select<br>
>   <a href="http://a.id" target="_blank">a.id</a> as id,<br>
>   a.uuid as uuid,<br>
>   a.progetto as progetto,<br>
>   a.procedimento as procedimento,<br>
>   a.geometria1 as geometria<br>
> from<br>
>   schemappa.repository_wfs as a<br>
> where<br>
>   a.geometria1 is not null;<br>
><br>
> I will know if discover something.<br>
> thx again.<br>
><br>
> 2016-11-16 8:54 GMT+01:00 Matthias Kuhn <<a href="mailto:matthias@opengis.ch" target="_blank">matthias@opengis.ch</a>>:<br>
> > Hi Andrea,<br>
> ><br>
> > It works here.<br>
> > Do you have an instead of insert rule written on your view?<br>
> > It works for me this way (but my views are too complicated for postgres<br>
> > to create default insert rules by default anyway).<br>
> > You can also try to alter your view with a default value for the column<br>
> > in question.<br>
> ><br>
> > Best regards<br>
> > Matthias<br>
> ><br>
> > On 11/16/2016 08:15 AM, Andrea Peri wrote:<br>
> >> Hi,<br>
> >> I'm having some trouble to edit a postgis view using qgis.<br>
> >><br>
> >> The view is a simple:<br>
> >> select list-of-field from table1;<br>
> >><br>
> >> And the table1 is a table with a Primary key on a serial type field.<br>
> >><br>
> >> I add the first record and all was ok. The commit too.<br>
> >> When go to add the second record.<br>
> >> I start to have trouble.<br>
> >> The qgis report my always a constraint violation of the pk.<br>
> >> I try to leave the pk field empty, or try to fill it with a value.<br>
> >> No solution work.<br>
> >><br>
> >> So my question is if is confirmed that qgis (I use the 2.18) version)<br>
> >> is working in editing with a postgis view .<br>
> >><br>
> >> Thx.<br>
> >> A.<br>
> >><br>
> >><br>
> >><br>
> > ______________________________<wbr>_________________<br>
> > Qgis-user mailing list<br>
> > <a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
> > List info: <a href="http://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">http://lists.osgeo.org/m<wbr>ailman/listinfo/qgis-user</a><br>
> > Unsubscribe: <a href="http://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">http://lists.osge<wbr>o.org/mailman/listinfo/qgis-<wbr>user</a><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> Qgis-user mailing list<br>
> <a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
> List info: <a href="http://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">http://lists.osgeo.org/mailman<wbr>/listinfo/qgis-user</a><br>
> Unsubscribe: <a href="http://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">http://lists.osgeo.org/mailman<wbr>/listinfo/qgis-user</a></p></blockquote></div></div></div></div></blockquote></div>
</div></div>