Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fid regenerate on GPKG vector layer exported #32934

Merged
merged 1 commit into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerexporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ QgsVectorLayerExporter::QgsVectorLayerExporter( const QString &uri,
if ( sinkFlags.testFlag( QgsFeatureSink::SinkFlag::RegeneratePrimaryKey ) && path.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
{
QString fidName = options.value( QStringLiteral( "FID" ), QStringLiteral( "FID" ) ).toString();
int fidIdx = vectorProvider->fields().lookupField( fidName );
int fidIdx = fields.lookupField( fidName );
if ( fidIdx != -1 )
{
mOldToNewAttrIdx.remove( fidIdx );
Expand Down
35 changes: 35 additions & 0 deletions tests/src/python/test_provider_ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,41 @@ def testRegenerateFid(self):
fids = set([f['fid'] for f in lyr.getFeatures()])
self.assertEqual(len(fids), 4)

def testExportWithoutFids(self):
""" Test export with a feature without fid, regression GH #32927

This test case is related to testRegenerateFid
"""

fields = QgsFields()
fields.append(QgsField('one', QVariant.Int))
fields.append(QgsField('two', QVariant.Int))
tmpfile = os.path.join(self.basetestpath, 'testExportWithoutFids.gpkg')
options = {}
options['update'] = True
options['driverName'] = 'GPKG'
options['layerName'] = 'output'
exporter = QgsVectorLayerExporter(tmpfile, "ogr", fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem(4326), False, options, QgsFeatureSink.RegeneratePrimaryKey)
self.assertFalse(exporter.errorCode(),
'unexpected export error {}: {}'.format(exporter.errorCode(), exporter.errorMessage()))

feat = QgsFeature(fields)

feat['one'] = 100
feat['two'] = 200
feat.setGeometry(QgsGeometry.fromWkt('point(4 45)'))
exporter.addFeature(feat)

del exporter
# make sure layers exist
lyr = QgsVectorLayer('{}|layername=output'.format(tmpfile), "lyr1", "ogr")
self.assertTrue(lyr.isValid())
self.assertEqual(lyr.crs().authid(), 'EPSG:4326')
self.assertEqual(lyr.wkbType(), QgsWkbTypes.Point)
feat_out = next(lyr.getFeatures())
self.assertEqual(feat_out.attribute('two'), 200)
self.assertEqual(feat_out.attribute('one'), 100)

def testTransaction(self):

tmpfile = os.path.join(self.basetestpath, 'testTransaction.gpkg')
Expand Down