The short version here is that using an unqualified lastval is a bad idea. Triggers, rules, etc. May cause problems.
You should completely avoid lastval . Using:
BEGIN; INSERT INTO "users" ("email", "first_name", "last_name", "password", "objectstate_id", "activate_rid") VALUES (' xpress@carepilot.com ', 'Xpress', 'Care', 'f9fecdd84ee071806423adf30d6d6ff04e1a0a2c6688f2c057ddbab1d6b55d02', 4, 'EMQHTMMvViAB5BdYj0E6') RETURNING id;
where id follows the name of the generated key column.
This approach will handle multi-valued inserts and INSERT INTO ... SELECT ... correctly, and there will be no problems with sequence triggers.
If you must use a function call approach, at least use currval('tablename_id_seq') (passing the appropriate sequence name) instead of lastval .
source share