javaapache-commons-dbutils

Apache DBUtils QueryRunner returning ids from wrong column in insert


I am using Apache DBUtils

Long rowId = queryRunner.insert(sql, new ScalarHandler<Long>(), params);

My table schema is

CREATE TABLE abc
(
    userid bigint,
    api_key text,
    key_id integer NOT NULL DEFAULT nextval('api_keys_key_id_seq'::regclass),
    CONSTRAINT api_keys_pkey PRIMARY KEY (key_id),
    CONSTRAINT userid_fkey FOREIGN KEY (userid)
    REFERENCES public.users (userid) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION
)

Problem is rowId is coming from userid column where as primary key of table is key_id and i want the returning id of insert query to be from key_id column.


Solution

  • The solution which i followed was to create table again with key_id as first column. In this way key_id was returned in response to insert query. There may be an option to configure this but i was unable to find.

    I know creating a table again for this is not good approach but anyone found answer please do share.