Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Does "setval()" Fail With "relation ... does not exist"?

Tags:

postgresql

If you attempt to set a sequence number like this:

SELECT setval('table_ID_seq', (SELECT max("ID") + 1 FROM table));

You might encounter the following error:

ERROR:  relation "table_ID_seq" does not exist
LINE 1: SELECT setval('table_ID_seq', (SELECT max("ID") + 1 FROM t...
                      ^

********** Error **********

ERROR: relation "table_id_seq" does not exist
SQL Status:42P01
like image 662
Arc Avatar asked Jan 23 '13 16:01

Arc


1 Answers

The problem is that PostgreSQL will normalize identifier names unless they are put in double quotes.

However, this will not work:

SELECT setval("table_ID_seq", (SELECT max("ID") + 1 FROM table));

Instead, you will have to put single-quotes around the double-quoted text:

SELECT setval('"table_ID_seq"', (SELECT max("ID") + 1 FROM table));
like image 192
Arc Avatar answered Oct 03 '22 17:10

Arc