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

PG: identify pk when exporting #45375

Merged
merged 1 commit into from
Oct 4, 2021
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
17 changes: 16 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4384,8 +4384,23 @@ Qgis::VectorExportResult QgsPostgresProvider::createEmptyLayer( const QString &u
}

// get the pk's name and type
// Try to find a PK candidate from numeric NOT NULL / UNIQUE columns
if ( primaryKey.isEmpty() )
{
for ( const auto &field : std::as_const( fields ) )
{
if ( field.isNumeric() &&
( field.constraints().constraints() & QgsFieldConstraints::Constraint::ConstraintUnique ) &&
( field.constraints().constraints() & QgsFieldConstraints::Constraint::ConstraintNotNull ) &&
( field.constraints().constraints() & QgsFieldConstraints::ConstraintOrigin::ConstraintOriginProvider ) )
{
primaryKey = field.name();
break;
}
}
}

// if no pk name was passed, define the new pk field name
// if no pk name was passed or guessed, define the new pk field name
if ( primaryKey.isEmpty() )
{
int index = 0;
Expand Down
37 changes: 37 additions & 0 deletions tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -3387,6 +3387,43 @@ def testPkeyIntArray(self):
self.assertTrue(feat.isValid())
self.assertEqual(feat["name"], "test")

def testExportPkGuessLogic(self):
"""Test that when creating an empty layer a NOT NULL UNIQUE numeric field is identified as a PK"""

md = QgsProviderRegistry.instance().providerMetadata("postgres")
conn = md.createConnection(self.dbconn, {})
conn.executeSql(
'DROP TABLE IF EXISTS qgis_test."testExportPkGuessLogic_source" CASCADE')
conn.executeSql(
'DROP TABLE IF EXISTS qgis_test."testExportPkGuessLogic_exported" CASCADE')
conn.executeSql(
"""CREATE TABLE qgis_test."testExportPkGuessLogic_source" ( id bigint generated always as identity primary key,
geom geometry(Point, 4326) check (st_isvalid(geom)),
name text unique, author text not null)""")

source_layer = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'id\' srid=4326 type=POINT table="qgis_test"."testExportPkGuessLogic_source" (geom) sql=', 'testExportPkGuessLogic_source', 'postgres')

md = QgsProviderRegistry.instance().providerMetadata('postgres')
conn = md.createConnection(self.dbconn, {})
table = conn.table("qgis_test", "testExportPkGuessLogic_source")
self.assertEqual(table.primaryKeyColumns(), ['id'])

self.assertTrue(source_layer.isValid())

# Create the URI as the browser does (no PK information)
uri = self.dbconn + ' sslmode=disable srid=4326 type=POINT table="qgis_test"."testExportPkGuessLogic_exported" (geom) sql='

exporter = QgsVectorLayerExporter(uri, 'postgres', source_layer.fields(), source_layer.wkbType(), source_layer.crs(), True, {})
self.assertTrue(exporter.lastError() == '')

exported_layer = QgsVectorLayer(self.dbconn + ' sslmode=disable srid=4326 type=POINT table="qgis_test"."testExportPkGuessLogic_exported" (geom) sql=', 'testExportPkGuessLogic_exported', 'postgres')
self.assertTrue(exported_layer.isValid())

table = conn.table("qgis_test", "testExportPkGuessLogic_exported")
self.assertEqual(table.primaryKeyColumns(), ['id'])

self.assertEqual(exported_layer.fields().names(), ['id', 'name', 'author'])


if __name__ == '__main__':
unittest.main()