I am using Loopback 4 first time and using MySQL as DB connector. I am able to use models with all my code but faced issue for id columns with generated: true
.
First using required: true
in my models is not allowing to add data using API Calls.It giving me issue id can't be blank
. I able to ignore this error if I made required: false
That actually not a big issue as I expecting MySQL to generate value using AUTO INCREMENT.
But now I need to generate some fixture data using a single API. In that case, I need to manually insert data into id
column something like
await this.projectRepository.create({
id: 1,
title: "My Project"
createdAt: (new Date).toISOString()
});
await this.projectItemsRepository.create({
id: 1,
projectId: 1,
title: "My Item 1"
createdAt: (new Date).toISOString()
});
await this.projectItemsRepository.create({
id: 2,
projectId: 1,
title: "My Item 2"
createdAt: (new Date).toISOString()
});
So Project Id is foreign key in projectitems model. Now if I execute this script, I am getting issue for id id can't be set
. Look like loopback 4 blocking attempt to set value in case of id column is generated.
First using
required: true
in my models is not allowing to add data using API Calls.
This is expected as this parameter is taken from the OpenAPI 3.0 spec, which works from the perspective of the API consumer (i.e. the API consumer MUST provide the parameter, not necessarily the ORM).
So Project Id is foreign key in projectitems model. Now if I execute this script, I am getting issue for id
id can't be set
.
Set the Model configuration settings.forceId: false
:
@model({
settings: {
forceId: false,
}
})
class MyModel extends Entity {/* ... */}
References: