Cfquery not returning generated key

Hi everyone,

So I’m running into a weird issue where cfquery is not returning the generated key via #insertq.generatedKey# after doing an insert operation. trying to pull it seems to more or less lead to a loop somewhere–the webpage never finishes loading and either continues forever or leads to a 502 error when accessing via the reverse proxy.

More interestingly, when trying to do a cfdump on queries, it says that The key [INSERTQ] does not exist.

DB is Postgres 11.

Any thoughts?

@weppy, Please make sure your table has an auto increment column, then only we can get generatedkey from the result without error after inserting data.

Hi @cfmitrah,

Thanks for the reply. Yes, it has an id column of type serial.

@weppy,

I’ve checked with using serial data type. It gives generatedkey using PostgreSQL DB also.
I tested with according to the below example. Could you please check this? If possible :slight_smile:

CREATE TABLE test ( id serial, f_name varchar(20) )

<cfquery name ="insertData" datasource="testDs" result="res">
	INSERT INTO test ( name ) VALUES ( 'lucee' )
</cfquery>
<cfdump var="#res.generatedkey#" />
1 Like

@cfmitrah,

Thank you so much–you’ve helped me figure it out. I wasn’t including the “result=” part in cfquery–I thought just using “name=” was enough as I can loop through SELECTs just based on that.

It’s been a while since I’ve been doing CF and I’m trying to get back up to speed!

Thanks again!!

1 Like

@weppy, Yes, You are right. Actually, we can use the “name” attribute for the select statement. If we use the insert statement, you should follow the “result” attribute. :slight_smile: