Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

currval Function in PostgreSQL complaining that "column does not exist"

Tags:

postgresql

I am trying to use PostgreSQL's currval function to return the last inserted row id of a table called Concept. Concept has a serial primary key called cid and there was an automatically generated Sequence called Concept_cid_seq.

I try the following statement and get an error:

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 table with one row (as I'd expect) showing columns like last_value, start_value, etc...

What am I missing here? Am I passing the wrong information to currval? Why does it say the 'column does not exist?'

like image 944
Drewmate Avatar asked Nov 02 '11 18:11

Drewmate


People also ask

What is the use of not in in PostgreSQL?

NOT IN condition is basically used to retrieve data from the table by excluding specified values from the column. We can also use the NOT IN condition with not equal and the AND operator in PostgreSQL. We can also write the NOT IN query by using the not equal and the AND operator.

What is PostgreSQL column does not exist error?

PostgreSQL column does not exist exception occurs when we have used column did not exist in the table or it will occur when the used column name has lower case name and we have used upper case in our query. We can avoid this exception in many ways like double-quote the column name for which column we have to get the exception.

How to execute the not condition with the SELECT statement in PostgreSQL?

To execute the NOT condition with the select statement in PostgreSQL we need to have select privileges on the table or we need to have super user privileges to execute the NOT IN condition statement.

What should I avoid when writing a PostgreSQL question?

But avoid … Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience. To learn more, see our tips on writing great answers. Not the answer you're looking for? Browse other questions tagged postgresql join select group-by or ask your own question.


1 Answers

It turns out that this was an issue with capitalization and quotes. Because I wanted to preserve the capitalization of the relation name I needed to use both single and double quotes in order to pass the correct relation name to currval.

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

like image 173
Drewmate Avatar answered Sep 21 '22 20:09

Drewmate