5

I am trying to set up an hasMany relation using a Mongo database. I have followed the guide to create an hasMany relation in the loopback 4 documentation (https://loopback.io/doc/en/lb4/HasMany-relation.html) and tryied to set differents properties but the foreign key custId is saved as a string and not as an ObjectID.

I also found a few other properties or options from others topics but people were using Loopback 3 and it doesn't seem to work with Loopback 4.

Did I miss something or is there any workaround ?

Here are my models :

@model()
export class Order extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: true,
  })
  id: string;

  @property({
    type: 'array',
    itemType: 'string',
    required: true,
  })
  product: string[];

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

  @property({
    type: 'string',
    id: true,
    generated: true,
  })
  custId: string;

  constructor(data?: Partial<Order>) {
    super(data);
  }
}


@model()
export class Customer extends Entity {
   @property({
      type: 'string',
      id: true,
      generated: true,
   })
   id: string;

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

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

  @hasMany(() => Order, {keyTo: 'custId'})
    orders?: Order[];

  constructor(data?: Partial<Customer>) {
    super(data);
  }
}
FabienCH
  • 51
  • 5
  • I don't know if the foreign key saved as a string has any relevance here but you can find a couple of issues about relations in the Loopback's GitHub repository: https://github.com/strongloop/loopback-next/labels/Relations And sorry for this link but I don't know exactly which of them is the most directly related with your problem. Even if you get to save the foreign keys as an ObjectId, I think that relations wouldn't work... – Wandeber Dec 18 '18 at 16:08

2 Answers2

1

This is currently a bug. The hasMany / belongsTo will end up saving the relation id as a string instead of an ObjectId. You can verify this by changing the id in the database directly to an ObjectId and then it will find it.

Reference: https://github.com/strongloop/loopback-next/issues/2085

It is also mentioned on the latest Monthly Milestone here, so hopefully it will be resolved soon: https://github.com/strongloop/loopback-next/issues/2313

Edit: I was able to get it working by adding strictObjectIDCoercion to the model, but that can break other things according to issue 2085 linked above.

@model({
  settings: {
    strictObjectIDCoercion: true,
  }
})
codephobia
  • 1,580
  • 2
  • 17
  • 42
  • Hello @codephobia, could you explain a bit more about the solution like after adding the setting do you change the id from string to objectid or keep it as such? – Vikranth May 05 '19 at 16:22
  • @Vikranth Just adding the setting to the model seems to resolve the issue. No need to change the id (from string to ObjectId or vice versa). Make sure you read through the issue thread though to make sure it wont affect other parts of your app. – codephobia May 07 '19 at 02:30
0

For hasMany relationship you need to update order Model.

Update order.model with :

1.Import Customer Model

import {Customer} from './customer.model';

remove custId: string;

2.For reference customer Id Just update code with

@belongsTo(() => Customer)
  custId: number;

Reference Example : here

IftekharDani
  • 3,619
  • 1
  • 16
  • 21
  • Did you make it work ? I've tried and i add the same issue, custId were stored has string and not ObectID. Anyway I decided to use UUID instead of ObjectID to get rid of this problem. – FabienCH Oct 11 '18 at 07:22