lastInsertId在Postgresql中不起作用 [英] lastInsertId does not work in Postgresql

查看:84
本文介绍了lastInsertId在Postgresql中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是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列通常是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');

这篇关于lastInsertId在Postgresql中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆