Database - Last insert ID

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

azaleas
Ebase User
Posts: 81
Joined: Thu Jul 30, 2015 12:44 pm

Database - Last insert ID

#1

Postby azaleas » Sun Feb 07, 2016 2:38 pm

I would like to fetch the id (auto incremented Primary Key) of last inserted element to DB. How can I do this?

Something like this:

Code: Select all

resources.ResourceName.insert();
var KeyID = last_inserd_id;

ID is generated with MySQL auto increment.
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#2

Postby Steve James » Sun Feb 07, 2016 5:48 pm

The field that is mapped to your pk column should hold the value post insert.
0 x

azaleas
Ebase User
Posts: 81
Joined: Thu Jul 30, 2015 12:44 pm

#3

Postby azaleas » Mon Feb 08, 2016 6:49 am

Thanks Steve. I got it
0 x

AJDulk
Ebase User
Posts: 94
Joined: Fri Sep 14, 2007 12:18 pm
Location: The Netherlands
Contact:

Re: Database - Last Insert ID

#4

Postby AJDulk » Fri Jan 27, 2017 2:34 pm

I have an Apache Derby database with an auto-increment column that is mapped in a non-table Database Resource as follows:
- Field Name: ID
- Type: BIGINT
- Length: 19
- Required: true
- Unique Key: true
- Read Only: true
- Persistent: true
- Dynamic SQL: false

Select columns: *
Select from tables: ADDRESSES
Where Clause: ID=&&ID

When I do an insert I am getting:
... ERROR ... com.ebasetech.xi.exceptions.FormRuntimeException: Field ID of resource dbAddress is required but has no value

If I turn off required then it does the insert but does not give back the ID.

I would think the required would be ignored at insert on a read-only field and I would expect the auto-increment ID to be returned but it does not seem to be working as such.

Any idea what I have done wrong that it is not working?

Here is what I have found about Derby and AutoIncrement on the web: https://db.apache.org/derby/docs/10.5/r ... togen.html
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#5

Postby Jon » Fri Jan 27, 2017 3:37 pm

The Ebase field mapped to the auto-increment column will have the new value after the insert.
0 x

AJDulk
Ebase User
Posts: 94
Joined: Fri Sep 14, 2007 12:18 pm
Location: The Netherlands
Contact:

#6

Postby AJDulk » Fri Jan 27, 2017 4:42 pm

That is not the case. I am getting a null back or cannot even insert the record as per the description above.

This could be a Derby specific bug in Ebase.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#7

Postby Jon » Fri Jan 27, 2017 5:27 pm

Sorry, I didn't see your latest post before I replied.

An auto-increment column should have:
read only: true
persistent: true
required: false

From what you say, it inserts OK like that so presumably the database increments the column successfully. If you have debug turned on for the resource you should see a line in the log something like:

DEBUG auto-increment column ID set with value xxx

If you don't see this line, it would imply that it's not working with Derby.
0 x

AJDulk
Ebase User
Posts: 94
Joined: Fri Sep 14, 2007 12:18 pm
Location: The Netherlands
Contact:

Re: Database - Last Insert ID

#8

Postby AJDulk » Mon Jan 30, 2017 10:36 am

Here is the log:

Code: Select all

Debug: Debug for database resource dbAddress - SQL statement:
Debug: insert into ADDRESSES (POSTCODE_NUMBER,STREET,NUMBER,POSTCODE_LETTERS,EXTRA) values (?,?,?,?,?)
Debug:   parameter1 : value 1000
Debug:   parameter2 : value Postbus
Debug:   parameter3 : value 1
Debug:   parameter4 : value AA
Debug:   parameter5 : value null
Debug: End execution of command - insert  : 11:01:04.655
Info : ** END EXECUTION OF RESTFUL WEB SERVICE getAddressNL using endpoint: byPostcodeNumber **
So nothing about the ID is there.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#9

Postby Jon » Tue Jan 31, 2017 9:51 am

Yes you're right, it doesn't work with Derby. This is because Derby is returning false to DatabaseMetadata.supportsGetGeneratedKeys() so Ebase assumes the database doesn't provide support for generated keys. I've raised this as a bug as Derby does in fact provide some support (probably enough support) to return a single auto-increment column.
0 x


Who is online

Users browsing this forum: Google [Bot] and 40 guests