pgsql中非自增主键表insert时报错

浏览:996 发布日期:2018/06/10 分类:ThinkPHP5专区 关键字: bug pgsql
【测试环境】
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;
            }

        }
最佳答案
评论( 相关
后面还有条评论,点击查看>>