I am developing a QGIS plugin, in which I want the user to be able to add/delete features from some layers. Here is the code I'm using to delete a feature with id = trayId
try: self.traysLayer.startEditing() self.traysLayer.deleteFeatures([trayId]) self.traysLayer.commitChanges() except Exception: self.iface.messageBar().pushMessage("Error", "The selected tray can't be deleted because it is in use.")
The problem is sometimes the feature to be deleted is referenced by another table using foreign key, so I'm getting the following PostGIS error:
ERROR: update or delete on table "tray" violates foreign key constraint
I have tried adding try/except to handle all exceptions, but it doesn't succeed to handle that error, I am still getting the same error.
How can I handle such error?