I'm setting up an api using LoopBack 4 and its mysql connector. So I have a model Plane, which has a @hasMany relation with pilots :
class Plane extends Entity {
@property({
id: true,"dataPrecision": 10, "dataScale": 0, "nullable": "N" },
})
id: number;
@property()
name: string;
@hasMany(() => Pilot, { keyTo: 'planeId' })
pilots?: Array<Pilot>;
So now what I wanted to do is to create a plane and add it its pilots on a single request. In my plane repository I made something like this :
class PlaneRepository extends DefaultCrudRepository<...> {
planes;
constructor(
@inject('datasources') dataSource: any,
@repository.getter('PilotRepository') getPilotRepository
) {
this.planes = this.createHasManyRepositoryFactoryFor('planes', getIpilotRepository);
}
And my controller looks like that:
class PlaneController {
@post('/project', {
responses: {
'200': {
description: 'Plane model instance',
content: { 'application/json': { schema: getModelSchemaRef(Project) }
},
},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Plane, { exclude: ['id'], includeRelations: true }),
},
},
})
plane: Omit<Plane, 'id'>,
): Promise<plane> {
return this.planeRepository.create(plane);
}
}
But when I try to call my route with something like that
{ name: 'Jet 27', pilots: [ { id: 0, name: Chuck Berry } ] }
I have a 422 error:
"The
Plane
instance is not valid. Details:pilots
is not defined in the model (value: undefined)."
I don't know if this is the expected behaviour, I must admit that I'm a bit confused with the relations way of functionning, but if it is, then how am I supposed to do.