Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres query for sequence containing camel case

Tags:

postgresql

I want to set a sequence in my table. So I listed all sequences with

SELECT c.relname 
FROM pg_class c 
WHERE c.relkind = 'S';

which gives me e.g.: tableName_id_seq

when I try to set the sequence using

SELECT setval("tableName_id_seq", (SELECT MAX(id) from "tableName"));

I get:

ERROR:  relation "tableName_id_seq" does not exist
LINE 1: SELECT setval('tableName_id_seq', (SELECT MAX(id) from "t...

I already tried single and double quotes. Anyone any idea what to do here?

like image 715
Karl Adler Avatar asked Jul 03 '26 19:07

Karl Adler


1 Answers

Avoid creating identifiers in camel case. It's really boring to work with it:

CREATE SEQUENCE "CamelCaseSeq";
SELECT nextval(format('%I', 'CamelCaseSeq'));
SELECT setval(format('%I', 'CamelCaseSeq'), 6666);
SELECT currval(format('%I', 'CamelCaseSeq'));
SELECT * FROM "CamelCaseSeq";

A quote from the docs:

Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other.

More info here and here.

like image 94
Michel Milezzi Avatar answered Jul 05 '26 13:07

Michel Milezzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!