Getting error while adding records

Hello,
I am getting following error while using it with Postgres, when I add any record I am getting

Message: pg_fetch_result(): Argument #1 ($result) must be of type PgSql\Result, bool given

It was working fine few days back but now its showing error, we also have unique id for table as well so not sure what is wrong, we tested but unable to resolve,

Any help really appreciated.

Thanks

An uncaught Exception was encountered

Type: TypeError

Message: pg_fetch_result(): Argument #1 ($result) must be of type PgSql\Result, bool given

Filename: /var/www/html/application/libraries/GroceryCrudEnterprise/laminas/laminas-db/src/Adapter/Driver/Pgsql/Connection.php

Line Number: 291

Hello @ondigit ,

I guess there is an issue with the database itself? Maybe you have changed something? The error means that you are getting a Postgres error in the database and hence returning “false”. Maybe there is more info within the error itself? Maybe sending the full error is showing extra information?

Regards
Johnny

Sure
— Full Error —

An uncaught Exception was encountered

Type: TypeError

Message: pg_fetch_result(): Argument #1 ($result) must be of type PgSql\Result, bool given

Filename: /var/www/html/application/libraries/GroceryCrudEnterprise/laminas/laminas-db/src/Adapter/Driver/Pgsql/Connection.php

Line Number: 291

<p>Backtrace:</p>

	
		<p style="margin-left:10px">
		File: /var/www/html/application/libraries/GroceryCrudEnterprise/laminas/laminas-db/src/Adapter/Driver/Pgsql/Connection.php<br />
		Line: 291<br />
		Function: pg_fetch_result			</p>
	

	
		<p style="margin-left:10px">
		File: /var/www/html/application/libraries/GroceryCrudEnterprise/laminas/laminas-db/src/Adapter/Driver/Pgsql/Pgsql.php<br />
		Line: 223<br />
		Function: getLastGeneratedValue			</p>
	

	
		<p style="margin-left:10px">
		File: /var/www/html/application/libraries/GroceryCrudEnterprise/grocery-crud/enterprise/src/GroceryCrud/Core/Model.php<br />
		Line: 515<br />
		Function: getLastGeneratedValue			</p>
	

	
		<p style="margin-left:10px">
		File: /var/www/html/application/libraries/GroceryCrudEnterprise/grocery-crud/enterprise/src/GroceryCrud/Core/State/InsertState.php<br />
		Line: 94<br />
		Function: insert			</p>
	

	
		<p style="margin-left:10px">
		File: /var/www/html/application/libraries/GroceryCrudEnterprise/grocery-crud/enterprise/src/GroceryCrud/Core/State/StateAbstract.php<br />
		Line: 1177<br />
		Function: GroceryCrud\Core\State\{closure}			</p>
	

	
		<p style="margin-left:10px">
		File: /var/www/html/application/libraries/GroceryCrudEnterprise/grocery-crud/enterprise/src/GroceryCrud/Core/State/StateAbstract.php<br />
		Line: 331<br />
		Function: stateOperation			</p>
	

	
		<p style="margin-left:10px">
		File: /var/www/html/application/libraries/GroceryCrudEnterprise/grocery-crud/enterprise/src/GroceryCrud/Core/State/InsertState.php<br />
		Line: 104<br />
		Function: stateOperationWithCallbacks			</p>
	

	
		<p style="margin-left:10px">
		File: /var/www/html/application/libraries/GroceryCrudEnterprise/grocery-crud/enterprise/src/GroceryCrud/Core/GroceryCrud.php<br />
		Line: 2867<br />
		Function: render			</p>
	

	
		<p style="margin-left:10px">
		File: /var/www/html/application/controllers/Admin.php<br />
		Line: 7130<br />
		Function: render			</p>
	

	

	
		<p style="margin-left:10px">
		File: /var/www/html/index.php<br />
		Line: 315<br />
		Function: require_once			</p>

Hello @ondigit ,

From a quick google search it seems that the table is not creating automatically and id for the table. My blind guess would be:

  1. You have changed the database columns and accidentally removed the auto generated column
  2. The auto generated column is working fine but you’ve ended up to have the highest value possible and it can’t generate a new one.

The solutions for the above:

  1. Adding an auto-generated column to a table:
ALTER TABLE your_table_name
ADD COLUMN id SERIAL PRIMARY KEY;
  1. If you suspect that the auto-generated column is not functioning correctly due to reaching the maximum value:
-- Find the maximum value of the id column
SELECT MAX(id) FROM your_table_name;

-- If the maximum value is close to the maximum limit of the data type, 
-- you might want to reset the sequence
-- Suppose the sequence name is something like "your_table_name_id_seq"
-- Resetting the sequence would reset the auto-increment counter
-- Replace "your_table_name_id_seq" with the actual sequence name
SELECT setval('your_table_name_id_seq', (SELECT MAX(id) FROM your_table_name) + 1);

Regards
Johnny

Hello Johnny,

We have already added SERIAL PRIMARY KEY, and few days back it was working fine, suddenly its showing this error.

Do we need to update the GC to latest version or something like that ?

Kindly advise.

Thanks

Hello @ondigit ,

Not sure that the update will wok but what version do you currently have? Latest one is 3.1.1. Maybe it is worth update anyway :slight_smile:

Also can you give us more information about the PHP version, Postgres version that you use?

Regards
Johnny

Hello Johnny,

We able to fix issue by passing the last inserted ID to the function, so all is working as expected.
Appreciate for your time and support.

Thanks

1 Like