Auto increment on id field when adding new row

Auto increment on id field when adding new row

TommyDTTommyDT Posts: 6Questions: 2Answers: 0
edited May 2022 in Free community support

As the title says when I press "add" button then on "add entry" form I need id field to be automatically filled with nextval of ID field from DB. But now the field is empty as shown on the screenshot below:

I also need the id field to be readonly but I already found the way to do this.
Currently I am trying to fill id field by calling function that performs ajax request to the back-end side but id does not work.
Here is back-end code (Spring boot + spring data):

Controller:

Repository:

Front-end side:

Can you help me so I could fix it?

Answers

  • tokrishtokrish Posts: 12Questions: 1Answers: 0

    @TommyDT Remove the Id from add form and let the DB add this for you. Make your Id key primary + autoIncrement.

    Why? Many reasons but one particular would be, the value you've received from getLotMaxId() will be invalid as soon as someone else has added a new record! Unless the whole operation is happening inside a transaction you cannot rely on it.
    If DB can already do this for you, why do you want to reinvent the wheel?

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    Agreed. You could query the database to find out what the next in the sequence will be, but I'd say this is a really bad idea (without a bunch of extra provisions, such as back channel updates, and throwing that value away in case of conflict). Multiple users (or even just multiple tabs to the page) would cause a major problem with this.

    Allan

Sign In or Register to comment.