How to insert record and return the identity value with the module sdk

How does one do the equivalent of system.db.runUpdateQuery or runPrepUpdate with getKey=1 within the sdk? According to the SRConnection docs SRConnection, there is a runPrepInsertGetKey, but it returns:

java.lang.NoSuchMethodError: 'int com.inductiveautomation.ignition.gateway.datasource.SRConnection.runPrepInsertGetKey(java.lang.String, java.lang.String, java.lang.String, java.lang.Object)'

This error occurred on 8.1.12 and 8.1.15

That looks like you're calling an instance method as a static method. You need to obtain an SRConnection first, probably by going into a DatasourceManager off of the GatewayContext provided in your module's setup hook.

It is being called as an instance method. The call works when calling 'runPrepUpdate', but returns the number of records created (1), not the Id.

newKey = __db.connection().runPrepInsertGetKey(query, ...

What language are you writing your module in? Can you make your typing explicit and/or see what you actually have after __db.connection()?

Unless you’re calling com.inductiveautomation.ignition.gateway.datasource.DatasourceManager#getConnection(java.lang.String), I’m not sure you actually have an SRConnection.

runPrepInsertGetKey returns long starting with Ignition 8.1.6.

1 Like

That was it! When we started the modules the target gateway was back at 8.0.13, and the modules were built accordingly. This particular call was in a shared jar file which had upgraded references. Once all version references were updated and updated modules installed, the call to runPrepInsertGetKey runs successfully.

Is this in fact the method used to implement the system.db.runPrepUpdate with getKey=1? runPrepInsertGetKey requires the table and column to be passed in, which is not part of the python interface.

runUpdateQuery and runPrepUpdate use it and pass in null values for table and column, despite what the Javadoc says about it…