-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
many to many Relations #5054
Comments
Hi @moweex
Your schema is fine, it works as expected, we tested with this sample app and your schema (minus the Note the order of the saves in the
Schema type Task @model {
id: ID!
name: String!
icon: String
records: [RecordTask] @connection(name: "TaskRecords")
}
type TimeTrackingRecord @model {
id: ID!
hours: Int!
minutes: Int!
time: AWSDateTime!
# building: Building @connection(name: "BuildingTimeTrackingRecords")
tasks: [RecordTask] @connection(name: "RecordTasks")
}
type RecordTask @model {
id: ID!
record: TimeTrackingRecord @connection(name: "RecordTasks")
task: Task @connection(name: "TaskRecords")
} App (.tsx) import React, { useEffect, useState } from "react";
import "./App.css";
import { DataStore } from "@aws-amplify/datastore";
import { Task, TimeTrackingRecord, RecordTask } from "./models";
function App() {
const [recordTasks, setRecordTasks] = useState([] as RecordTask[]);
useEffect(() => {
queryRecordTasks();
return () => {};
}, []);
async function queryRecordTasks() {
const recordTasks = await DataStore.query(RecordTask);
setRecordTasks(recordTasks);
}
async function addRecordTask() {
const tasks = [];
for (let i = 0; i < 5; i++) {
const task = new Task({
name: `the task ${i + 1}`
});
await DataStore.save(task);
tasks.push(task);
}
const record = new TimeTrackingRecord({
hours: 2,
minutes: 30,
time: new Date().toISOString()
});
await DataStore.save(record);
for (const task of tasks) {
const recordTask = new RecordTask({
task,
record
});
await DataStore.save(recordTask);
}
await queryRecordTasks();
}
return (
<div className="App">
<header className="App-header">
<button onClick={addRecordTask}>Add</button>
<pre style={{ textAlign: "left" }}>
{JSON.stringify(recordTasks, null, 2)}
</pre>
</header>
</div>
);
}
export default App; I hope this helps, let us know how it goes. |
Hi @manueliglesias , thank you very much for your response and demo application, maybe my question was not clear enough. Savings are working fine with our schema, we are using it the exact way you mentioned in your code. however, our issue is after saving a recordTask, if you query TimeTrackingRecord, you will find that the tasks property is always empty. const records = await DataStore.query(TimeTrackingRecord); for (const record of records) { so it seems the save function not updating the other end of the relationship, even after a sync call with appsync, tasks still empty, to get it we are doing it manually by querying RecordTask with filter. |
@moweex : Hi, thank you for bring this up. Currently, we only support loading from the one part of 1:M relationship. For eq, if you have Post to Comment as 1:M relationship. You can query for the post from comment but not the other way around. But, we are tracking this internally to eager/lazy load all many side from the one side(comments from post). We will mark this issue as feature request to track updates. |
Please note, that similar problem is reported also in amplify-cli (however, it wasn't directly related to DataStore): |
Any update on this? I was quite excited about the prospects of DataStore, it's API, and how it works, however without this ability I cannot use it at all within my app. Being able to lazy-load relations would obviously be the preferred method to match the capabilities of GraphQL, but I would even just settle for the ability to manually pull records by filtering on IDs at this point. It feels like that fix primarily involves adjusting the generated models to include the foreign ID so that the predicate filter can be used with |
I just ran into this issue as well. I'm building an application for tracking books. I've got an index page that shows all of the books and their authors. Books have a M:M relationship to authors through a Here's what I'm doing, my index pages queries books and then I loop over each of those books and query their BookAuthors. The code looks something like this... books = await DataStore.query(Book)
bookAuthorQueries = books.map(book => async (
(await DataStore.query(BookAuthor)).filter(ba => ba.book.id === book.id)
)) Does this seem like the best approach for querying has many relationships? Also, will this approach stop working once I reach a certain number of records? Are there any limits I should be aware of? This is the first app I've written using Dynamo so sorry if this is a silly question. |
Any update about this? |
Is there any rough timeline for this "lazy-loading" feature? |
Do we have a date for the release of the lazy loading feature? Very disappointing that such a basic feature has not been implemented and has not received attention. |
Would be nice to get at least a short update about your plans.... |
@ryanto we're using the same approach as you (as a stop-gap?). The default page size limit on // TEMP FIX(?)
DataStore.query(BookAuthor, Predicates.ALL, {
page: 0,
limit: 1_000_000
}); |
Agreed on hearing of an update, I can't help but think about the serious performance hit I take when querying the join table to get data from both sides of the relationship.. @smithad15 's comment: #5054 (comment) seems to best capture my thoughts on a solution as well. Seems like there would have to be another index table generated in DDB for each of the two Objects being joined in the M-M relation. Each Object's model would then include the FK for when the object is queried (this is a M-M anyways, expect a bit higher of a performance hit when querying an index for the FK) Rather than getting both objects returned in the BookAuthor join table (see @ryanto 's #5054 (comment)), we would then be able to query only against the Author object without having to duplicate the effort by querying both Book and Author. tl;dr Thoughts? |
guys, updates? |
Hi, |
Querying for relations seem to work well when using predicates: const author = await DataStore.query(Author, authorID);
const books = await DataStore.query(Book, book => book.authorID("eq", authorID)); This, at least to me, seems preferable over the use of |
Has anyone found a workaround for this? It's sort of laughable that you'd have to query Parent A to get the ID and use that to query related Child B. Doesn't that sort of defeat the point of relations in the first place? Unless you can query related records, relations are basically just honorary. |
My workaround is this: https://gist.github.com/ThomasRooney/89faeed810d1d18dfa16d0dd16eef0b2 It is a react hook that will resolve the connection before it returns the full structure. It will also only follow 1 layer of connections, but when you have a deep structure you can layer them next to each other, similiar to @arabold's answer. Interface for returning many items looks like (for a N:M connection, sorted):
|
🤬 |
Still no updates on this ? |
It sucks. Just to get child connections, I need to create VMs and manually fetch parent id and then fetch child records based on paren id. There should be an option of whether user wants to load data eagerly or lazily. |
Lazy Loading was released late last year, does this help? https://aws.amazon.com/blogs/mobile/new-lazy-loading-nested-query-predicates-for-aws-amplify-datastore/ |
@undefobj Unfortunately no. I have models somewhat like these: enum MembershipLevel { type Membership @model @auth(rules: [{allow: public}]) { type EGTopic @model @auth(rules: [{allow: public}]) {
return await DataStore.query(Membership, m => m.) What should I type after m. ? There is no topic id which I can use to compare. I don't see any documentation for 1:1 relationship.
I would appreciate your quick response. |
@amitchaudhary140 The You can follow the migration guide to update your CLI version + schema to take advantage of the V5 library. Once you migrate your schema and update your libs+app accordingly, your IDE's autocompletion/intellisense should help a great deal in completing that query for you. It should guide you to write something like this: await DataStore.query(Membership, m => m.id.eq(topic.membershipId)) Or simply this: await topic.membership; To try this out, your next steps would be:
And then, of course, play around with lazy loading and nested predicates and let us know how it goes! |
@svidgen Still no luck. FYI, I copied the models from AWS Studio. So how does the schema change take place over there? |
The steps from my previous comment are intended to be run locally. Use the And, if it's not clear from the migration guide, you'll want to perform an |
I performed below steps:
I believe I don't have to go and do manual change in schemas (as mentioned in migration guide) as I had deleted them and they were rebuilt when pulled. But I can't still access membership from topic. Not sure what I am missing. Should I delete whole backend? Below is what it looks like after performing all steps. |
The step that looks like it's missing from your list is After that, you can upgrade your |
Thanks @svidgen . It worked till now. I have one more question that if we have two models generated (Eager and Lazy) then why can't we load data eagerly? DataStore should provide some way to load data eagerly. |
** Which Category is your question related to? **
Datastore
** What AWS Services are you utilizing? **
AWS AppSync,
** Provide additional details e.g. code snippets **
We are implementing an RN application using AWS Amplify, we have a schema with a many to many relationships, as suggested we used a bridge model to link the tow models
Example Schema:
type Task
@model
{
id:ID!
name: String!
icon: String
records: [RecordTask] @connection(name: "TaskRecords")
}
type TimeTrackingRecord
@model
{
id: ID!
hours: Int!
minutes: Int!
time: AWSDateTime!
building: Building @connection(name: "BuildingTimeTrackingRecords")
tasks: [RecordTask] @connection(name: "RecordTasks")
}
type RecordTask
@model
{
id: ID!
record: TimeTrackingRecord @connection(name:"RecordTasks")
task: Task @connection(name: "TaskRecords")
}
Here RecordTask is used to link both Task & TimeTrackingRecord, we used amplify codegen models to generate models for us.
To complete our logic we are doing the following using datastore
1- Creating a set of Tasks
2- Creating a TimeTrackingRecord
3- loop over tasks and creating RecordTask
Then we tried two solutions:-
1- Updating already created TimeTrackingRecord to link it to the created RecordTask => Error on the update mutation by Appsync (no reason provided)
2- Relay that Datastore will connect the Tasks and TimeTrackingRecord, but unfortunately when we try to access TimeTrackingRecord tasks it is always empty however the RecordTask already created.
Any advice how can improve our M2M model to work fine with DataStore.
The text was updated successfully, but these errors were encountered: