def getDBLayerPrimaryKey(layer): uri = QgsDataSourceUri(layer.dataProvider().dataSourceUri()) return uri.key()