2

I am new to Loopback 4 and I made a model then data-source to MYSQL then repository then the controller (REST), then I visited http://127.0.0.1:3000/explorer/#/TodoController and went to POST section and then tried to insert some data I got:

Unhandled error in POST /todos: 500 Error: ER_NO_DEFAULT_FOR_FIELD: Field 'id' doesn't have a default value.

I searched the internet and found generated: true to auto increment the id field then I run npm run migrate and again inserted data in the POST section but again got the same error. I changed the A.I (auto-increment) field of id in MYSQL-DB to true then it inserted the data successfully.

All I want to make the id field auto-increment, how can I do that. I tried generated: true in todo.model.ts under id property but not working.

@property({
  type: 'number',
  id: true,
  generated: true,
})
id?: number;
ok.
  • 95
  • 1
  • 10

2 Answers2

2

If anyone is messing up with this issue just move generated: true before id: true as:

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

A pretty fix :)

ok.
  • 95
  • 1
  • 10
0

I tried the way as @ok answered but it's not working for me.

I am using MySQL db and I need to set a default value to 0. Then the issue is ok. werid.

lwin
  • 3,784
  • 6
  • 25
  • 46