I want to perform some business validation of the data being inserted in a table and I need to check data in another table when doing so. The way to this seems to be using a BEFORE INSERT FOR EACH ROW TRIGGER in PostgreSQL and from the function it calls I can return NULL if the new data fails validation to prevent it being inserted.
Is there any way I can return an error message describing the validation error as well?
Use
raisefor exceptions:So you can say things like this in your trigger:
That will abort the trigger and (usually) the current transaction. The message that you raise should make its way back to the client application.