0

I am trying to use currVal function in postgres embedded db to get the value I have inserted in an insert statement in the same session. Please find the below code for the same.

Student Table Definition:

CREATE TABLE student (
    id bigserial NOT NULL,
    name varchar(255) NOT NULL,
    number int8 NOT NULL,
);
CREATE TABLE student_course (
    student_id foreign_key references student(id),
    course_name varchar(255) NOT NULL
);

Query To Insert Student:

insert into student (name,number)
values("Student1","55");

Query to fetch id column:

insert into student_course
select currval(pg_get_serial_sequence('student','id')), 'course1';

Both the insert queries run in a single session, but I get null value when using currVal function to get the inserted id.

The same queries work fine with actual database but doesn't work in embedded extension. I am using postgis docker image for embedded db.

  • The `select` is missing a ')' for the function, is that a copy and paste error? What happens if you run just `select currval(pg_get_serial_sequence('student','id'));`? – Adrian Klaver Oct 29 '21 at 15:41
  • Yes it's a copy paste error. If I run a select currval(pg_get_serial_sequence('student','id')), it gets executed successfully and gives me a null value. I also tried with first executing a nextval and then currval to avoid session related errors but the result is same. – Harshil Thakkar Nov 02 '21 at 06:45
  • What version of Postgres? What do you mean by 'embedded extension'? Add answers as update to your question. – Adrian Klaver Nov 02 '21 at 14:57
  • @AdrianKlaver, I am using in memory postgresql database for writing tests. See the below maven artifact for reference and version details. https://mvnrepository.com/artifact/com.opentable.components/otj-pg-embedded/0.13.1 – Harshil Thakkar Jan 28 '22 at 10:24
  • I have to believe this is coming from the plugin. I would file an issue here [Issues](https://github.com/opentable/otj-pg-embedded/issues?q=is%3Aissue+is%3Aopen). – Adrian Klaver Jan 28 '22 at 16:00
  • Thanks @AdrianKlaver for that. It would be really helpful if you can link that issue if you have already raised it. I could not find it in the open or closed issues. – Harshil Thakkar Feb 07 '22 at 08:02
  • No, I said you should file an issue. I have not done so and and have no plans to do so. – Adrian Klaver Feb 07 '22 at 15:42

0 Answers0