question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create bidirectional (inverse) one-to-one relationships?

See original GitHub issue

For example, let’s say I have a Person and a Passport tables. One person has only one passport and one passport belongs to one person.

schema.js

    tableSchema({
      name: "persons",
      columns: [
        { name: "passport_id", type: "string" },
        ...
      ]
    }),
    tableSchema({
      name: "passports",
      columns: [
        { name: "person_id", type: "string" },
        ...
      ]
    }),

Person.js

export default class Person extends Model {
  static table = "persons";

  @relation("passports", "passport_id") passport;
  ...
}

Passport.js

export default class Passport extends Model {
  static table = "passports";

  static associations = {
    persons: { type: "belongs_to", key: "person_id" }
  };
  @relation("persons", "person_id") person;
  ...
}

I create a new passport:

database.collections.get("passports").create(passport => {
  passport.person.set(person);
  // ...
});

However, I would like being able to access passport from person as well. Is manually updating person.passport the only way? With Realm I believe there is a way to it with linking objects properties.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
radexcommented, Feb 11, 2019

Perhaps it would be nice to have the @children equivalent for to-one relationships? (@child?)

Perhaps! TBH I’m not sure if to-one relations (as opposed to just single belongs_to relation) is idiomatic. Because you’re creating two sources of truth. If you have two objects that are supposed to point at each other, but one of them points at something else, it’s en error. It’s easier if just one of the objects points at the other (say, Passport points at Person). This way, say, Person, would only have a helper getter to find its Passport, based on a query.

Here’s an example of this:

  @lazy
  _currentUserQuery: Query<User> = this.collections
    .get(Tables.users)
    .query(Q.where(Columns.users.isMe, true))

  @lazy
  currentUser: Observable<User> = this._currentUserQuery.observe().pipe(
    switchMap(([user]) => {
      invariant(user, `No user marked as isMe=true found`)
      return user.observe()
    }),
  )

It would be nice if this could be minified to a single line, using a decorator. If you want to implement something to that point, let me know, and I can give you pointers as to how 😃

0reactions
cwagner22commented, Feb 9, 2019

Perhaps it would be nice to have the @children equivalent for to-one relationships? (@child?)

I’ve found a way to access passport from person without having to set the id manually (to person) by adding this to the Person model:

  @lazy passport = this.collections
    .get("passports")
    .query(Q.where("person_id", this.id));

Unfortunately this returns an array of results so I have to do that in my code, which is quite ugly: const passport = this.props.passport[0];

Read more comments on GitHub >

github_iconTop Results From Across the Web

Direction in Entity Relationships (The Java EE 6 Tutorial)
The inverse side of a bidirectional relationship must refer to its owning side by using the mappedBy element of the @OneToOne, @OneToMany, or...
Read more >
What is “the inverse side of the association” in a bidirectional ...
For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key.
Read more >
Hibernate Tips: How to map a bidirectional many-to-one ...
In this week's Hibernate Tip, I show you how to map a bidirectional many-to-one association so that you can use it in queries...
Read more >
JPA One to One Uni/Bi Directional Relationship Tutorial
This tutorial describes how to create a unidirectional and bidirectional OneToOne relationships using JPA annotations.
Read more >
How to synchronize bidirectional entity associations with JPA ...
Learn how to synchronize bidirectional @OneToMany, @OneToOne, and @ManyToMany entity associations when using JPA (Java Persistence API) and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found