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

Alexandre Neto senhor.neto at gmail.com
Thu Mar 29 03:28:19 PDT 2018


Thank you Nyall!

That solved the problem. I failed to see that in the C++ code for split
features tool, but it was there.

The only small annoyance that I had was the need to convert the result of
feature.attributes() into a dictionary.


Once again, thanks! It solved a long-standing issue in the plugin.

Alex

But it for the clarification.
A qua, 28/03/2018, 23:01, Nyall Dawson <nyall.dawson at gmail.com> escreveu:

> On 28 March 2018 at 08:32, Alexandre Neto <senhor.neto at gmail.com> wrote:
> > 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 ...
>
> The canonical/correct 3.0 way to do this is to use
> QgsVectorLayerUtils.createFeature
>
> That method should be used whenever you are creating a brand new
> feature which is destined for a particular vector layer, and needs to
> correctly respect constraints and default value clauses set on the
> provider side.
>
> Nyall
>
-- 
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/20180329/8017702d/attachment.html>


More information about the QGIS-Developer mailing list