[GIS] Edit a view of a PostGIS table in QGIS

postgispostgresqlqgissql-view

I have a simple layer in PostGIS created as follows:

CREATE TABLE layer (
    id serial PRIMARY KEY,
    geom geometry(LINESTRING, 31370),
    ...
)

To avoid duplicate information, I created a view with some information (eg length) derived from the geometry:

CREATE VIEW myview AS
    SELECT *,
    ST_LENGTH(geom) AS length
    FROM layer;

I can access and edit the view myview in QGIS but whenever I want to save my edits, I got this error message: "Could not commit changes to layer. PostGIS error while adding features: ERROR: cannot insert into column "length" of view "myview". DETAIL: View columns that are not columns of their base relation are not updatable."

Is there a way to edit and save a view of a PostGIS table in QGIS?

Best Answer

You need an INSTEAD OF trigger to handle / ignore length attribute.

Related Question