Skip to content
Browse files

DBManager: use the first suitable field loading a view (fix #5676)

  • Loading branch information
brushtyler committed Jun 9, 2012
1 parent 2921588 commit 53b57d1441ea8be8f764cbe3d92e9395b081bea0
Showing with 11 additions and 5 deletions.
  1. +1 −1 python/plugins/db_manager/
  2. +10 −4 python/plugins/db_manager/db_plugins/
@@ -27,7 +27,7 @@ def description():
return "Manage your databases within QGis"

def version():
return "0.1.19"
return "0.1.20"

def qgisMinimumVersion():
return "1.5.0"
@@ -549,7 +549,8 @@ def uri(self):
uri = self.database().uri()
schema = self.schemaName() if self.schemaName() else ''
geomCol = self.geomColumn if self.type in [Table.VectorType, Table.RasterType] else QString()
uri.setDataSource(schema,, geomCol if geomCol else QString())
uniqueCol = self.getValidQGisUniqueFields(True) if self.isView else None
uri.setDataSource(schema,, geomCol if geomCol else QString(), QString(), if uniqueCol else QString() )
return uri

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

ret = []

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

# add both serial and int4 fields with an unique index
# then add both oid, serial and int fields with an unique index
indexes = self.indexes()
if indexes != None:
for idx in indexes:
if idx.isUnique and len(idx.columns) == 1:
fld = idx.fields()[ idx.columns[0] ]
if fld and fld not in ret and fld.dataType in ["oid", "serial", "int4"]:
if fld.dataType in ["oid", "serial", "int4", "int8"] and fld not in ret:
ret.append( fld )

# and finally append the other suitable fields
for fld in self.fields():
if fld.dataType in ["oid", "serial", "int4", "int8"] and fld not in ret:
ret.append( fld )

if onlyOne:
return ret[0] if len(ret) > 0 else None
return ret

0 comments on commit 53b57d1

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