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 do bi-directional one to one relationship?

See original GitHub issue

** Which Category is your question related to? ** graphql-transformer

** What AWS Services are you utilizing? ** AppSync / Dynamo

** Provide additional details e.g. code snippets **

I’m currently running into a situation where initially I had a model like follows:

type Post @model {
    id: ID!
    title: String!
}

type Response @model {
    id: ID!
    content: String!
    post: Post @connection
}

This correctly stores a ā€˜responsePostId’ for each row in the Response table.

And then I decided I wanted access to ā€˜response’ from the Post model so I modified the schema to the following:

type Post @model {
    id: ID!
    title: String!
    response: Response @connection(name: "PostResponse")
}

type Response @model {
    id: ID!
    content: String!
    post: Post @connection(name: "PostResponse")
}

What this does is create a ā€œpostResponseIdā€ attribute for every new model in the Posts table and uses $context.source.postResponseId to get Post.response. However I don’t need that index, because to get the response for a post (through the Post.response resolver) I should just use $context.source.id and as the value for "responsePostId’ in the Response data source.

Note if I change the Post schema to a one to many relationship:

type Post @model {
    id: ID!
    title: String!
    responses: [Response] @connection(name: "PostResponse")
}

The Post.responses resolver correctly queries using $context.source.id rather than a $context.source.postResponseId.

I read https://aws-amplify.github.io/docs/cli/graphql?sdk=js and it talks about how to do bidirectional one-to-many relationship and a simple one-to-one relationship but there is no example in there for a bidirectional one-to-one. Am I missing something here. Maybe one-to-one bidirectional relationships are not supported because there is no way to enforce the strict 1:1ness in the database?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:21
  • Comments:25 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
gxxcastillocommented, Oct 6, 2022

Hi @ykethan, following up on this, I can see how the @hasOne relationship on type A results in a reference to a related model, B, and how the @belongsTo sets up a relationship back to A.

However, I don’t see how to enforce the bi-directional relationship in the sense that creating type A, with a reference to a new type B doesn’t seem to result in the relationship from B to A getting created. As far as I can tell, I need to ensure I update the appropriate field in B to point back to A? If so, is this something I should be able to do with a BatchPutItem that creates both A and B at the same time, with references to each other?

2reactions
gautamkshitijcommented, Apr 19, 2020

Any ideas? šŸ˜… or Do we wait for amplify maintainers??

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hibernate Bidirectional One-to-One example using @ ...
In this page, we see how to implement bidirectional One-to-One mapping between persisted objects using @OneToOne annotation. A one-to-one relationships ...
Read more >
Hibernate - bidirectional @OneToOne
The way around this is to tell Hibernate that there will ALWAYS be a value there, and NEVER a null value. Knowing this,...
Read more >
Hibernate One-To-One Bidirectional with Shared Primary ...
Step 1: Create required Database Table Ā· Step 2: Create project directory structure Ā· Step 3: Update pom. Ā· Step 4: Create Model...
Read more >
JPA + Hibernate - Bidirectional @OneToOne example
Just as with unidirectional one-to-one relationship, bidirectional one-to-one relationship has a single target object reference in theĀ ...
Read more >
one-to-one bidirectional association - Java Tutorials
In a relational database in a one-to-one relationship, a row in table X can have no more than one matching row in table...
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