From aaff41752de5008471bb05ac4577657743601577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Ho=C5=A1ko?= Date: Thu, 24 Sep 2020 07:52:17 +0200 Subject: [PATCH] Error if the table haven't primary key If thy table haven't primary key, and i pass $returnID = false, than the ci4 throw this error: ``` CRITICAL - 2020-09-24 00:47:00 --> pg_query(): Query failed: ERROR: lastval is not yet defined in this session #0 [internal function]: CodeIgniter\Debug\Exceptions->errorHandler(2, 'pg_query(): Que...', '/var/www/ci4/sy...', 196, Array) #1 /var/www/ci4/system/Database/Postgre/Connection.php(196): pg_query(Resource id #9, 'SELECT LASTVAL(...') #2 /var/www/ci4/system/Database/BaseConnection.php(741): CodeIgniter\Database\Postgre\Connection->execute('SELECT LASTVAL(...') #3 /var/www/ci4/system/Database/BaseConnection.php(669): CodeIgniter\Database\BaseConnection->simpleQuery('SELECT LASTVAL(...') #4 /var/www/ci4/system/Database/Postgre/Connection.php(519): CodeIgniter\Database\BaseConnection->query('SELECT LASTVAL(...') #5 /var/www/ci4/system/Model.php(887): CodeIgniter\Database\Postgre\Connection->insertID() #6 /var/www/ci4/app/Models/MyModel.php(46): CodeIgniter\Model->insert(Array, false) #7 /var/www/ci4/app/Controllers/MyController.php(113): App\Models\MyModel->new_connection('1', '1') #8 /var/www/ci4/app/Controllers/MyController.php(54): App\Controllers\MyController->do_create_connection() #9 /var/www/ci4/system/CodeIgniter.php(918): App\Controllers\MyController->create_connection() #10 /var/www/ci4/system/CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\MyController)) #11 /var/www/ci4/system/CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false) #12 /var/www/ci4/public/index.php(45): CodeIgniter\CodeIgniter->run() #13 {main} CRITICAL - 2020-09-24 00:47:00 --> Uncaught CodeIgniter\Format\Exceptions\FormatException: Failed to parse json string, error: "Type is not supported". in /var/www/ci4/system/Format/Exceptions/FormatException.php:9 Stack trace: #0 /var/www/ci4/system/Format/JSONFormatter.php(71): CodeIgniter\Format\Exceptions\FormatException::forInvalidJSON('Type is not sup...') #1 /var/www/ci4/system/API/ResponseTrait.php(414): CodeIgniter\Format\JSONFormatter->format(Array) #2 /var/www/ci4/system/API/ResponseTrait.php(134): CodeIgniter\Debug\Exceptions->format(Array) #3 /var/www/ci4/system/Debug/Exceptions.php(168): CodeIgniter\Debug\Exceptions->respond(Array, 500) #4 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler(Object(ErrorException)) #5 {main} thrown #0 [internal function]: CodeIgniter\Debug\Exceptions->shutdownHandler() #1 {main} ``` This will skip to getting inserted ID --- system/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Model.php b/system/Model.php index 76d103347f47..239d980834a8 100644 --- a/system/Model.php +++ b/system/Model.php @@ -876,7 +876,7 @@ public function insert($data = null, bool $returnID = true) ->insert(); // If insertion succeeded then save the insert ID - if ($result->resultID) + if ($returnID && $result->resultID) { if (! $this->useAutoIncrement) {