'generated: true not working in loopback 4
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;
Solution 1:[1]
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 :)
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ok. |
| Solution 2 | lwin |
