Currval Function in PostgreSQL complains that "column does not exist"

I am trying to use the PostgreSQL currval function to return the last inserted row id of a Concept table row. Concept has a sequential primary key called cid and an automatically generated sequence has appeared called Concept_cid_seq .

I try the following statement and get an error message:

 SELECT currval("Concept_cid_seq"); ERROR: column "Concept_cid_seq" does not exist LINE 1: SELECT currval("Concept_cid_seq"); ^ ********** Error ********** ERROR: column "Concept_cid_seq" does not exist SQL state: 42703 Character: 16 

But when I run the query:

 SELECT * from "Concept_cid_seq"; 

I get a single row table (as you would expect) showing columns like last_value, start_value, etc.

What am I missing here? Am I passing wrong information to currval? Why does he say that "the column does not exist?"

+6
source share
1 answer

It turns out that this was a problem with capitalization and quotation marks. Since I wanted to preserve the capitalization of the relation name, I had to use single and double quotes to pass the correct relation name to currval .

I changed the query to SELECT currval('"Concept_cid_seq"'); (pay attention to external single quotes) and it worked correctly.

+5
source

Source: https://habr.com/ru/post/900637/


All Articles