Get relation object in express ts with knexjs and objection

31 Views Asked by At

i used express ts with knexjs and objection. i want get just name of each carBrand, carType, carTransamission. because i want passing the data into DTO. but i got error like this :

Property 'carBrand' does not exist on type 'CarModel'. Did you mean 'carBrandId'?

this is my CarModel :

export class CarModel extends Model {
  id!: string;
  name!: string;
  price!: number;
  year?: number;
  capacity!: number;
  description?: string;
  pictureUrl!: string;
  availableAt?: Date;
  createdAt!: Date;
  updatedAt!: Date;
  isDeleted!: boolean;
  deletedAt?: Date;
  carBrandId?: string;
  carTransmissionId?: string;
  carTypeId?: string;

  static relationMappings = {
    carBrand: {
      relation: Model.BelongsToOneRelation,
      modelClass: CarBrandModel,
      join: {
        from: "car.carBrandId",
        to: "car_brand.id",
      },
    },
    carTransmission: {
      relation: Model.BelongsToOneRelation,
      modelClass: CarTransmissionModel,
      join: {
        from: "car.carTransmissionId",
        to: "car_transmission.id",
      },
    },
    carType: {
      relation: Model.BelongsToOneRelation,
      modelClass: CarTypeModel,
      join: {
        from: "car.carTypeId",
        to: "car_type.id",
      },
    }
  };

  static get tableName() {
    return "car";
  }
}

export type Car = ModelObject<CarModel>;

i've followed in knexjs documentation i want get carBrand like this :

let car2 = await CarModel.query().first().withGraphFetched('carBrand');
    console.log(car2.carBrand.name)
0

There are 0 best solutions below