Skip to content

Commit 53b57d1

Browse files
committed
DBManager: use the first suitable field loading a view (fix #5676)
1 parent 2921588 commit 53b57d1

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

python/plugins/db_manager/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def description():
2727
return "Manage your databases within QGis"
2828

2929
def version():
30-
return "0.1.19"
30+
return "0.1.20"
3131

3232
def qgisMinimumVersion():
3333
return "1.5.0"

python/plugins/db_manager/db_plugins/plugin.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ def uri(self):
549549
uri = self.database().uri()
550550
schema = self.schemaName() if self.schemaName() else ''
551551
geomCol = self.geomColumn if self.type in [Table.VectorType, Table.RasterType] else QString()
552-
uri.setDataSource(schema, self.name, geomCol if geomCol else QString())
552+
uniqueCol = self.getValidQGisUniqueFields(True) if self.isView else None
553+
uri.setDataSource(schema, self.name, geomCol if geomCol else QString(), QString(), uniqueCol.name if uniqueCol else QString() )
553554
return uri
554555

555556
def mimeUri(self):
@@ -567,23 +568,28 @@ def toMapLayer(self):
567568
def getValidQGisUniqueFields(self, onlyOne=False):
568569
""" list of fields valid to load the table as layer in QGis canvas.
569570
QGis automatically search for a valid unique field, so it's
570-
needed only for queries (e.g. SELECT * FROM table LIMIT 1)"""
571+
needed only for queries and views """
571572

572573
ret = []
573574

574575
# add the pk
575576
pkcols = filter(lambda x: x.primaryKey, self.fields())
576577
if len(pkcols) == 1: ret.append( pkcols[0] )
577578

578-
# add both serial and int4 fields with an unique index
579+
# then add both oid, serial and int fields with an unique index
579580
indexes = self.indexes()
580581
if indexes != None:
581582
for idx in indexes:
582583
if idx.isUnique and len(idx.columns) == 1:
583584
fld = idx.fields()[ idx.columns[0] ]
584-
if fld and fld not in ret and fld.dataType in ["oid", "serial", "int4"]:
585+
if fld.dataType in ["oid", "serial", "int4", "int8"] and fld not in ret:
585586
ret.append( fld )
586587

588+
# and finally append the other suitable fields
589+
for fld in self.fields():
590+
if fld.dataType in ["oid", "serial", "int4", "int8"] and fld not in ret:
591+
ret.append( fld )
592+
587593
if onlyOne:
588594
return ret[0] if len(ret) > 0 else None
589595
return ret

0 commit comments

Comments
 (0)