how to make qgis layer editable after loading with postgres sql from database

19 Views Asked by At

Not sure why it is so hard to achieve (qgis 3.28):

Suppose I have a postgres database layer/table, with id as PK, geom as geometry column, and columns A, B, C. How to load this layer into Qgis with id as PK, geom as geometry column, but ONLY WITH COLUMN A and make loaded attribute table EDITABLE, so that it gets synched with database?

So far, with

uri = QgsDataSourceUri()
uri.setConnection("credentials")
uri.setDataSource('', "(select id, geom, A from schema.tablename)", 'geom', "", 'id')
vlayer = QgsVectorLayer(uri.uri(), name, etc)
QgsProject.instance().addMapLayer(vlayer)

I can add layer with columns that I want, but I cannot edit the attribute table (a view maybe? so that I cannot edit it?).

If I load the entire table as they are with:

uri.setDataSource(schema, tablename, "geom")

I can edit the table and get synched with database, but there will be extra columns.

How to do this at the qgis api level, or I need to tweak the database somehow?

0

There are 0 best solutions below