postgresql - Postgres not returning lastval() properly -
i trying insert new user our database, via psql in cli. when following:
start transaction; insert "users" ("email", "first_name", "last_name", "password", "objectstate_id", "activate_rid") values ('xpress@carepilot.com', 'xpress', 'care', 'f9fecdd84ee071806423adf30d6d6ff04e1a0a2c6688f2c057ddbab1d6b55d02', 4, 'emqhtmmvviab5bdyj0e6'); select lastval();
lastval
returns 39037, should technically 838. not inserting db reason. have googled , looked can think of , not getting answers. have idea going on here?
the short version here using unqualified lastval
bad idea. triggers, rules, etc can cause problems.
you should avoid lastval
entirely. use:
begin; insert "users" ("email", "first_name", "last_name", "password", "objectstate_id", "activate_rid") values ('xpress@carepilot.com', 'xpress', 'care', 'f9fecdd84ee071806423adf30d6d6ff04e1a0a2c6688f2c057ddbab1d6b55d02', 4, 'emqhtmmvviab5bdyj0e6') returning id;
where id
should name of generated key column.
this approach handle multi-valued inserts , insert ... select ...
correctly, , won't have issues triggers touching sequences.
if must use function-call based approach, @ least use currval('tablename_id_seq')
(passing appropriate sequence name) instead of lastval
.
Comments
Post a Comment