I have a point vector layer (Sensores) in my QGIS project, I also have a MySQL database layer (SensoresDB) containing only data. I have joined these two layers in order to geographically reference my MySQL data and classify it.


My problem is the data in my vector layer (Sensores) doesn't update when the data in the MySQL layer (SensoresDB) does. That's ok, I don't need it to update in the exact moment it changes, but it would be nice if I could force it with code. I've tried using layer.setCacheImage(None) and layer.triggerRepaint() to no avail. Until now, the only way it does update is when I close the project and open it again.

1 Answer 1


Might I suggest creating a macro which contains a function to rejoin both your layers. Using this method then allows you to run the function from the Python console whenever you want to update the vector layer.

You can do this by going to the toolbar:

Project > Project Properties > Macros

And then insert the following code:

shp = QgsMapLayerRegistry.instance().mapLayersByName( "Sensores" )[0]
mysql = QgsMapLayerRegistry.instance().mapLayersByName( "SensoresDB" )[0]

# Set JOIN properties by changing 'ID' to relevant field names
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = mysql.id()
joinObject.joinFieldName = mysqlField
joinObject.targetFieldName = shpField

def update():


Make sure that you save the project and enable macros by going to the toolbar:

Settings > General > Enable macros

Now the next time you reload the project and you edit your MySQL layer, just type update() in the Python console and it should rejoin and update your vector layer accordingly.

  • 1
    Well, I'm developing a plugin for my university and, as I'm not gonna be the end user, it has to be automatic, so I didn't use a macro. I instead put the same code to rejoin both layers on a thread that runs every 5 minutes. It works just fine. Thanks! Commented Apr 12, 2016 at 17:35
  • @JairNájeraSalaices - That's a cool idea! And most welcome =)
    – Joseph
    Commented Apr 13, 2016 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.