@@ -549,7 +549,8 @@ def uri(self):
549
549
uri = self .database ().uri ()
550
550
schema = self .schemaName () if self .schemaName () else ''
551
551
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 () )
553
554
return uri
554
555
555
556
def mimeUri (self ):
@@ -567,23 +568,28 @@ def toMapLayer(self):
567
568
def getValidQGisUniqueFields (self , onlyOne = False ):
568
569
""" list of fields valid to load the table as layer in QGis canvas.
569
570
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 """
571
572
572
573
ret = []
573
574
574
575
# add the pk
575
576
pkcols = filter (lambda x : x .primaryKey , self .fields ())
576
577
if len (pkcols ) == 1 : ret .append ( pkcols [0 ] )
577
578
578
- # add both serial and int4 fields with an unique index
579
+ # then add both oid, serial and int fields with an unique index
579
580
indexes = self .indexes ()
580
581
if indexes != None :
581
582
for idx in indexes :
582
583
if idx .isUnique and len (idx .columns ) == 1 :
583
584
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 :
585
586
ret .append ( fld )
586
587
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
+
587
593
if onlyOne :
588
594
return ret [0 ] if len (ret ) > 0 else None
589
595
return ret
0 commit comments