I'm creating a mini DB handling class and for convinience purposes, I want my generated INSERT queries are always try to return last insert id or null, if not applicable. I had tried to use:
INSERT INTO ... RETURNING lastval();
it works fine with tables which have a sequence, but if table have no sequence key field, that insert nothing and falls with error: lastval is not yet defined in this session
.
How to check in RERURNING
block, is sequence generator was used in this session or silently return last id or null, without error raising?
Note: for usablity purposes, I want to avoid direct specifing of PK field name.