Search code examples
mysqlloopbackjs

Loopback4 MySQL Auto-Increment ID


I've just installed Loopback4 (based on TS) and i'm trying to play around with it since it seems really easy to create an API using it. My question is rather simple (yet i dont know the answer). How can i increment the id of my model ?

Lets say i've got this in my model (created with lb4 model) ->

export class Post extends Entity {
  @property({
    type: 'number',
    id: true,
    required: false,
  })
  Id: number;

The first post (without adding ID) creates it with Id = 0. There's no next post since the Id doesnt auto-increment.

Any tip on how to do it? I'm using MySQL as stated in my title and LB4.

EDIT: Or even better, is there any way to modify the way Loopback4 (loopback-next) creates tables ? Theres one command that would need to be run after the DB tables have been created, something like ALTER TABLE post CHANGE Id Id INT(11) NOT NULL AUTO_INCREMENT;

I keep searching through the tree of options, but i've found nothing relevant and i dont think connecting to the db separately for this task is a good idea.


Solution

  • Fixed thanks to github. Apparently it is very similar to LB3 (which i didnt use).

      @property({
        type: 'number',
        id: true,
        generated: true,
      })
      Id: number;
    

    Adding generated: true turns on the auto-increment on mysql.