Skip to content
Permalink
Browse files

Fix import of shapefiles to postgres - regression introduced in 5abdfcb

… (#3652)

(cherry picked from commit a965a13)
  • Loading branch information
wonder-sk committed Nov 1, 2016
1 parent 629ca8b commit 5e4bb00fa3e11a5dc347ddb22ed0b98965b9b03c
Showing with 18 additions and 4 deletions.
  1. +3 −0 src/providers/postgres/qgspostgresprovider.cpp
  2. +15 −4 tests/src/python/test_provider_postgres.py
@@ -3526,6 +3526,9 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer( const Q
fldIdx = -1; // it is incremented in the for loop, i.e. restarts at 0
}
}

pkList = QStringList( primaryKey );
pkType = QStringList( "serial" );
}
else
{
@@ -471,17 +471,27 @@ def testImportKey(self):

def testKey(lyr, key, kfnames):
self.execSQLCommand('DROP TABLE IF EXISTS qgis_test.import_test')
uri = '%s table="qgis_test"."import_test" (g) key=\'%s\'' % (self.dbconn, key)
uri = '%s table="qgis_test"."import_test" (g)' % self.dbconn
if key is not None:
uri += ' key=\'%s\'' % key
err = QgsVectorLayerImport.importLayer(lyr, uri, "postgres", lyr.crs())
self.assertEqual(err[0], QgsVectorLayerImport.NoError,
'unexpected import error {0}'.format(err))
olyr = QgsVectorLayer(uri, "y", "postgres")
self.assertTrue(olyr.isValid())
flds = lyr.fields()
oflds = olyr.fields()
self.assertEquals(oflds.size(), flds.size())
for i in range(0, oflds.size()):
self.assertEqual(oflds[i].name(), flds[i].name())
if key is None:
# if the pkey was not given, it will create a pkey
self.assertEquals(oflds.size(), flds.size() + 1)
self.assertEquals(oflds[0].name(), kfnames[0])
for i in range(flds.size()):
self.assertEqual(oflds[i + 1].name(), flds[i].name())
else:
# pkey was given, no extra field generated
self.assertEquals(oflds.size(), flds.size())
for i in range(oflds.size()):
self.assertEqual(oflds[i].name(), flds[i].name())
pks = olyr.pkAttributeList()
self.assertEquals(len(pks), len(kfnames))
for i in range(0, len(kfnames)):
@@ -491,6 +501,7 @@ def testKey(lyr, key, kfnames):
testKey(lyr, '"f1"', ['f1'])
testKey(lyr, '"f1","F2"', ['f1', 'F2'])
testKey(lyr, '"f1","F2","f3"', ['f1', 'F2', 'f3'])
testKey(lyr, None, ['id'])


class TestPyQgsPostgresProviderCompoundKey(unittest.TestCase, ProviderTestCase):

0 comments on commit 5e4bb00

Please sign in to comment.
You can’t perform that action at this time.