Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using custom (underscored) keys for Relations : "Cannot read property 'target' of undefined" #2169

Closed
esoodev opened this issue Dec 18, 2018 · 8 comments · Fixed by #3326
Closed
Assignees
Labels
bug Relations Model relations (has many, etc.)

Comments

@esoodev
Copy link

esoodev commented Dec 18, 2018

Description

I am using an existing MySQL database and its schemas, and am following the tutorial to build relations.

Current Behavior

  • User has many Review
  • Review belongs to User

Due to the existing schema, Review has 'user_id' field that links to User's 'id.' I coded this relation in review.model.ts accordingly.

@belongsTo(() => User, {keyFrom: 'user_id', keyTo: 'id'})
user_id: number;

However, doing so gives me ...Cannot read property 'target' of undefined... error whenever ReviewRepository is called.
Modifying the code and its relevant parts like below does not cause any issues.

@belongsTo(() => User, {keyTo: 'id'})
userId: number;

Comments

How can I keep 'user_id' as a valid field for Review? Any help is appreciated.

@esoodev
Copy link
Author

esoodev commented Dec 18, 2018

Within ReviewRepository, instead of

this.user = this.createBelongsToAccessorFor('user', userRepositoryGetter);

I used

this.user = createBelongsToAccessor(
  {
    target: () => User,
    name: 'user',
    type: RelationType.belongsTo,
    source: Review,
    keyFrom: 'user_id',
    keyTo: 'id',
  },
  userRepositoryGetter,
  this,
);

This works for now, though not pretty. If anyone can provide an insight for making this code better, it'd be much appreciated!

@dhmlau dhmlau added the Relations Model relations (has many, etc.) label Dec 18, 2018
@b-admike b-admike added the bug label Jan 17, 2019
@JuliaRakitina
Copy link

JuliaRakitina commented Jan 18, 2019

+1 there is no clear documentation to build relations.

@JuliaRakitina
Copy link

JuliaRakitina commented Jan 18, 2019

Please improve documentation
here https://loopback.io/doc/en/lb4/BelongsTo-relation.html you wrote

call the createBelongsToAccessorFor function in the constructor of the source repository class with the relation name (decorated relation property on the source model) and target repository instance and assign it the property mentioned above.

In code snippets source model decorated prorepty is customerId , but
this.customer = this.createBelongsToAccessorFor(
'customer', - I suppose it should be customerId
customerRepositoryGetter,
);

@JuliaRakitina
Copy link

Ok, I managed documentation example to work. But I can't configure filter just to query just simple JOIN orders and its customers ((

@shadyanwar
Copy link

@JuliaRakitina I believe you are referring to #1352, which is a very important request IMO.

@shadyanwar
Copy link

shadyanwar commented Jan 22, 2019

this.user = createBelongsToAccessor(

Looks like the latest version of LB4 doesn't have createBelongsToAccessor()

@dhmlau dhmlau added the TOB label Jan 22, 2019
@samarpanB
Copy link
Contributor

For me, having this in review model

  @belongsTo(() => User)
  user_id: number;

and this in repository

this.user = this._createBelongsToAccessorFor(
      'user_id',
      userRepoGetter,
    );

works. Is there anything different you are trying to achieve ?

@bajtos bajtos added 2019Q2 and removed TOB labels Apr 12, 2019
@b-admike
Copy link
Contributor

b-admike commented May 8, 2019

Thank you all for chiming in. I've compiled the following criteria for this issue:

Acceptance Criteria

  • Review the issue and the detailed description to reproduce locally - write a test case in our repository package to simulate the behaviour / cover the use case.
  • Explore the proposed solutions and assess if this is a bug or a documentation update and follow up accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Relations Model relations (has many, etc.)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants