Linux Nginx
php版本:7.1.7
pgsql版本:10.3
pdo_pgsql版本:7.1.7
thinkphp版本:5.0/5.1
当一个数据表主键非自增字段时,使用db()->insert()方法插入成功,但是页面会报错,业务逻辑也没法往下走了。
SQLSTATE[55000]: Object not in prerequisite state: 7 ERROR: lastval is not yet defined in this session经查是在系统Query类insert方法中调用getLastInsID()时出现。我的解决方案是改为仅当$getLastInsID为true时才执行该方法。报错只出现在服务器,本地wamp下不会出现,不知道是pdo版本的问题。 // 执行操作
$result = 0 === $sql ? 0 : $this->execute($sql, $bind);
if ($result) {
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
$lastInsId = $this->getLastInsID($sequence);
if ($lastInsId) {
$pk = $this->getPk($options);
if (is_string($pk)) {
$data[$pk] = $lastInsId;
}
}
$options['data'] = $data;
$this->trigger('after_insert', $options);
if ($getLastInsID) {
return $lastInsId;
}
} 最佳答案