Loopback-next: `lb4 relation` doesn't allow to add both belongsTo and hasMany relation with the same 2 models

Created on 19 Jun 2019  路  6Comments  路  Source: strongloop/loopback-next

Description / Steps to reproduce / Feature proposal

If I have a CoffeeShop and Review model, supposedly I can add the following model relations:

  • CoffeeShop hasMany Review
  • Review belongsTo CoffeeShop

Using the lb4 relation command, after I add the hasMany relation where CoffeeShop is the source model and Review is the target model, the coffeeShopId property is added to the Review model, i.e.

  @property({
    type: 'number',
  })
  coffeeShopId?: number;

Then I tried to set the belongsTo relation. It showed the error (as shown below) that coffeeShopId already exists, and no belongsTo decorator being added.

$ lb4 relation
? Please select the relation type belongsTo
? Please select source model Review
? Please select target model CoffeeShop
? Source property name for the relation getter coffeeShopId
   create src/controllers/review-coffee-shop.controller.ts
Generation is aborted: Error: property coffeeShopId already exist in the model Review

Steps to reproduce

  1. Create the CoffeeShop and Review model using the
    https://github.com/dhmlau/loopback4-coffeeshop/blob/master/coffeeshop-model.json and https://github.com/dhmlau/loopback4-coffeeshop/blob/master/review-model.json

using the following command:

$ lb4 model coffeeshop-model.json --yes
$ lb4 model review-model.json --yes
  1. Create the datasource and repositories
  2. Run lb4 relation for the hasMany relation
$ lb4 relation
? Please select the relation type hasMany
? Please select source model CoffeeShop
? Please select target model Review
? Foreign key name to define on the target model coffeeShopId
? Source property name for the relation getter reviews
   create src/controllers/coffee-shop-review.controller.ts
  1. Then run lb4 relation again for the belongsTo relation.
    Then you'll get the above error.

Expected Behavior

If adding this manually, in the review model, the snippet around coffeeShopId would be changed from:

  @property({
    type: 'number',
  })
  coffeeShopId?: number;

to:

  @belongsTo(() => CoffeeShop)
  coffeeShopId: number;

Acceptance Criteria

  • [ ] fix belongsTo generator to be able to convert @property into @belongsTo decorator
2019Q3 CLI Relations bug p1

Most helpful comment

I think it makes sense if this is a common use case. If that's the case, I guess we should do it with hasOne too (even though it doesn't exist in lb4 relation yet)?

All 6 comments

@oleg269, since you've implemented the lb4 relation command, do you mind taking a look at this to see how we can get it resolved? Thanks!

cc @gczobel-f5 @bajtos

@dhmlau, while this isn't resolved, a workaround is to set the belongsTo relationship before the hasMany.

It makes me wonder - should we change the template for hasMany relation to set up @belongsTo relation on the target model from the beginning?

I think it makes sense if this is a common use case. If that's the case, I guess we should do it with hasOne too (even though it doesn't exist in lb4 relation yet)?

It makes me wonder - should we change the template for hasMany relation to set up @belongsTo relation on the target model from the beginning?

After some discussion during the estimate meeting, we are not sure about if we need the @belongsTo being created automatically after the @hasMany being added to a model. Because sometimes we want them to work independently. We might want to change the criteria for this issue.
Any thoughts? @bajtos @b-admike

I don't mind either way. Maybe the generator can ask the user whether they want to create belongsTo relation while creating hasMany? Either way, I think we need to fix belongsTo generator to be able to convert @property into @belongsTo decorator.

Was this page helpful?
0 / 5 - 0 ratings