[QGIS-Developer] PyQGIS - how to handle primary keys in geopackage

Alexandre Neto senhor.neto at gmail.com
Tue Mar 27 15:32:18 PDT 2018


My multipart split plugin (which I am porting to 3), It breaks multipart
features and transforms each part in individual features, copying the
attributes from the original feature.

In the process, I need to be careful with the autoincrement primary keys.
For PostGIS and Spatialite, asking for the providers pkattributesindex
works and I can do the following:

new_attributes = feature.attributes()

# When attribute is a Primary Key, replace by default value
### This is not working well with spatialite provider###
for j in provider.pkAttributeIndexes():
    if provider.defaultValue(j):
        new_attributes[j] = provider.defaultValue(j)
    else:
        new_attributes[j] = QVariant()

But for geopackage the provider.pkAttributeIndexes() always returns an
empty list...

The above code was copied a long time ago from the split features tools in
core. Looking at the code now, there is no longer that part where the
provider's primary keys are searched, Nevertheless, the code works and the
split features tool leave the pkattributes NULL to be autogenerated. Wasn't
able to figure out where that is done now ...

Thanks,

Alexandre Neto
-- 
Alexandre Neto
---------------------
@AlexNetoGeo
http://sigsemgrilhetas.wordpress.com
http://gisunchained.wordpress.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20180327/8a5315c1/attachment.html>


More information about the QGIS-Developer mailing list