When creating a virtual layer in QGIS, we can use the layer name of any existing layer in a query, like

SELECT layer1.*, layer2.*
FROM layer1
JOIN layer2 ON layer1.id = layer2.id

However layer names aren't necessarily unique. The docs say The query can use the name (or id) of existing vector layers. I assumed this would be the Identifier we can set in a layer's metadata options, but using these in a query results in a Referenced table layer_identifier in query not found! message. (Nor is it possible to use the identifier in the Source definition of an embedded layer: Query execution error on DROP TABLE IF EXISTS "table1"; CREATE VIRTUAL TABLE "table1" USING gsVLayer('spatialite','layer_identifier',UTF-8): 1 - Provider error).

We could use embedded layers, but then my problem is that we cannot use relative paths, as far as I can see (the project should be usable on different computers and OSes).

Is there a more robust mechanism than using layer names for creating a virtual layer?

1 Answer 1


You can get the layer ID from the layer properties / variables / layerID

Then the virtual layer can use it like

select * from lpr_000a16a_e20180319075213250

enter image description here

  • Ah yes. Upon seeing the metadata identifier field description popup it's clear that is meant for some external id. Too bad the layer_id variable isn't editable/overridable, it would be nice to get rid of the long generated string.
    – arjan
    Commented Mar 19, 2018 at 12:48

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.