Skip to content

hasOne() and belongsTo() not working in loopback4 #2209

@drplutus

Description

@drplutus

I have try with following code but i will got only first table record, there are missed second table info.

person.model.ts:

  @property({
     name: 'name',
    type: 'string',
  })
  name: string;

 @hasOne(() => Info)
  infos: Info;

info.model.ts:

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

  @belongsTo(() => Person)
   personId?: number;

person.repository.ts

 export class PersonRepository extends DefaultCrudRepository<Person,typeof Person.prototype.id> 
 {
     public readonly infos: HasOneRepositoryFactory<Info,typeof Person.prototype.id>;
     constructor(@inject('datasources.db') dataSource: DbDataSource,    
             @repository.getter('InfoRepository')
               getInfoRepository: Getter<InfoRepository>,) {
       super(Person, dataSource);
       this.infos = this._createHasOneRepositoryFactoryFor('infos',getInfoRepository);
    }
 }

Current Behavior

But when i will call the api using explorer they have return following schema

Example Value
 Schema
[  {
   "id": 0,
   "name": "string"
}]

Is there any solution for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    RelationsModel relations (has many, etc.)needs steps to reproduceIssues missing a small app and/or instructions for reproducing the problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions