0

I've created a view in SpatiaLite, the unique id is made with

cast(row_number() over() as integer) as id

the geometry is made with

CastToMultiPolygon(st_union(geom)) as geom (it's a merging operation)

and the geometry is registered with

INSERT INTO "main"."geometry_columns" ("f_table_name", "f_geometry_column", "geometry_type", "coord_dimension", "srid", "spatial_index_enabled") VALUES ('table1', 'geom', '6', '2', '27700', '0');

The layer shows in the QGIS canvas and all the features show in the attribute table, but selecting features is messed up. there appears to be no relationship between the canvas location and the feature selected - I click on a feature on the canvas and a completely different feature somewhere else is selected.

On trying to zoom to the feature from the attribute table I get

WARNING Zoom to feature id failed : Feature not found

and selection yields the spatialite error

WARNING SQLite error: no such column: ROWID SQL: SELECT 0,"description" FROM "table1" WHERE ROWID=7

As a test, I changed the query to declare the id column as "ROWID"

cast(row_number() over() as integer) as ROWID

then when I try to load the layer it won't load at all, complaining...

2024-07-26T13:10:36 WARNING SQLite error: no such column: id SQL: PRAGMA table_info("table1")

It seems the I'm not successfully telling QGIS what the unique id column is, but I cannot find any way to do so.

4
  • Welcome to GIS SE. As a new user, please take the Tour. This is completely expected behavior. Using row_number for a unique ID on anything but a materialized view will produce seemingly random results, because you've asked it to renumber every query, which means that any selection set based on a list of IDs will be useless in subsequent queries.
    – Vince
    Commented Jul 26 at 18:49
  • I don't understand your comment. I understand that a view with row_number() re-numbers each query, but all views change all data each query run, that's the point. Why would it suddenly become impossible to display this data (the unique_id), just because it changes each time the query is run? It seems counter-intuitive. Why not just run the query each time it's queried and display the results?
    – James
    Commented Jul 29 at 11:06
  • This gis.stackexchange.com/questions/25043/… question posted this gis.stackexchange.com/questions/12233/… upvoted answer which explicitly states "SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY column_to_sort_by ASC) AS ROW_NUMBER..." as a means of getting a unique_id. What am I missing?
    – James
    Commented Jul 29 at 11:11
  • Keep it simple and use just select a.rowid AS rowid as in gaia-gis.it/gaia-sins/Using-Views-Basic.pdf. The GDAL GeoPackage documentation has also a section about views gdal.org/drivers/vector/gpkg.html. If cast(row_number() over() as integer) as id happens to work otherwise it can easily break the connection from the main table to the spatial index table that is using the original ROWID.
    – user30184
    Commented Aug 6 at 22:11

0

Your Answer

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

Browse other questions tagged or ask your own question.