问题描述
我正在尝试使用PostgreSQL的 currval
函数返回名为 Concept
的表的最后插入的行ID。 Concept
有一个名为 cid
的串行主键,并且有一个自动生成的名为 Concept_cid_seq
。
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
但是当我运行查询:
SELECT * from "Concept_cid_seq";
我得到一张表,其中有一行(正如我期望的那样),其中显示诸如last_value,start_value,等等...
I get a table with one row (as I'd expect) showing columns like last_value, start_value, etc...
我在这里想念什么?我是否将错误的信息传递给currval?为什么说列不存在?
What am I missing here? Am I passing the wrong information to currval? Why does it say the 'column does not exist?'
推荐答案
事实证明,这是大写和引号引起的问题。因为我想保留关系名称的大写字母,所以我需要使用 both 单引号和双引号,以便将正确的关系名称传递给 currval
。
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
.
我将查询更改为 SELECT currval(' Concept_cid_seq');
(请注意外部单引号)并且可以正常工作。
I changed the query to SELECT currval('"Concept_cid_seq"');
(note the outer single quotes) and it worked correctly.
这篇关于PostgreSQL中的currval函数抱怨“列不存在”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!