If I have a CoffeeShop and Review model, supposedly I can add the following model relations:
hasMany ReviewbelongsTo CoffeeShopUsing 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
using the following command:
$ lb4 model coffeeshop-model.json --yes
$ lb4 model review-model.json --yes
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
lb4 relation again for the belongsTo relation.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;
belongsTo generator to be able to convert @property into @belongsTo decorator@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
@belongsTorelation 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.
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
hasOnetoo (even though it doesn't exist inlb4 relationyet)?