lastInsertId 在 Postgresql 中不起作用

2021-12-26 00:00:00 postgresql php pdo lastinsertid

我使用的是 Postgresql,当我想使用 PDO 来检索最新的插入 ID 时,我遇到了问题.这是我的代码:

I am using Postgresql, when I want to use PDO to retrieve the latest insertion ID, I got a problem. Here is my code:

$db->lastInsertId('columnName');

错误信息说

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "columnName" does not exist

我想我对 PHP 手册中所述的序列对象"有些误解.

I guess I have some misunderstanding about "sequence object" stated in the PHP Manual.

Note:

Returns the ID of the last inserted row, or the last value from a sequence object, 
depending on the underlying driver. For example, PDO_PGSQL() requires you to specify the 
name of a sequence object for the name parameter.

目前,columnName"是该自增属性的字符串.谁能指出我哪里出错了?谢谢.

Currently, the "columnName" is the string of that auto-incremented attribute. Can anyone point out where I went wrong? Thanks.

推荐答案

PostgreSQL 使用 序列 为serial 列和 serial 列通常用于 PostgreSQL 中的自动递增"列.序列有名称,并且通常独立于任何特定的表,因此您可以使用一个序列为多个不同的表生成唯一的 ID;序列名称是 lastInsertId 想要作为其参数的名称:

PostgreSQL uses sequences to generate values for serial columns and serial columns are generally what is used for "auto-incrementing" columns in PostgreSQL. Sequences have names and are, in general, independent of any particular table so you could have one sequence generating unique IDs for several different tables; the sequence name is what lastInsertId wants as its argument:

例如,PDO_PGSQL() 要求您为 name 参数指定序列对象的名称.

For example, PDO_PGSQL() requires you to specify the name of a sequence object for the name parameter.

PostgreSQL创建的序列对象自动命名为[table]_[column]_seq,所以:

The sequence object created by PostgreSQL is automatically named [table]_[column]_seq, So:

$id = $db->lastInsertId('tableName_columnName_seq');

相关文章