2

I'm writing a plugin in PyQGIS 3, and I've to test for vector joins before run a code, so I've found here about vectorJoin() and write this:

def joinTables(self):
    d, f = self.lyrPair()  # d = {target;layerToJoin}
    for k, v in d.items():
        target = QgsProject.instance().mapLayer(k)
        layerToJoin = QgsProject.instance().mapLayer(v)
        v_lyr = QgsProject.instance().mapLayers().values()

        for lyr in v_lyr:
            joinsInfo = lyr.vectorJoins()

            if not joinsInfo:
                QMessageBox.information(iface.mainWindow(), "Success", 
                                "No previously joins!")
                # do some stuff here
            else:
                QMessageBox.critical(iface.mainWindow(), "Error", 
                                "Previously Joins exists!")
                # ask for remove join

        fieldToJoin = QgsProject.instance()
        symb = QgsVectorLayerJoinInfo()
        symb.setJoinFieldName('id_feature')
        symb.setTargetFieldName('id')
        symb.setJoinLayerId(layerToJoin.id())
        symb.setUsingMemoryCache(True)
        symb.setEditable(True)
        symb.setDynamicFormEnabled(True)
        symb.setUpsertOnEdit(True)
        symb.setPrefix('')
        symb.setJoinLayer(layerToJoin)
        target.addJoin(symb)
        layerToJoin.startEditing()
        target.triggerRepaint()

but in both case, when has joined and when hasn't joined, the code above returns that there is. First, vectorJoins() returns a list of joined layers and I've to test every single layer, the second I've missed something in this code?

  • 1
    The target layers are not mentioned when using vectorJoins(), only the layer which you have set to have the join. Are you wanting to identify both the join layer and the target layer? – Joseph Mar 26 '19 at 10:41
  • @Joseph, I've suppressed the initial code, now here it's. I've two types of layers: vector layers and table layers both from PostgreSQL, and the join will be made between then. The idea is identify any existent join and asks to user if he wants to continue with or remove previous join and continue. I'm a begginer in pyqgis and I'm very confuse for testing about existent joins. – Francisco Camello Mar 26 '19 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.