How to create bidirectional (inverse) one-to-one relationships?
See original GitHub issueFor 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:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Perhaps! TBH I’m not sure if
to-one
relations (as opposed to just singlebelongs_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:
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 😃
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:
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];