DataStore cannot delete `hasOne` relationships
See original GitHub issueDescribe the bug
DataStore cannot delete hasOne
relationships. It breaks at IndexedDBAdapter.deleteTraverse
when trying to find the recordToDelete
of the HAS_ONE
relationType.
Unhandled Runtime Error
NotFoundError: Failed to execute 'index' on 'IDBObjectStore': The specified index was not found.
To Reproduce
- Init Amplify. Add GQL API. Enable DataStore.
- Define the Schema below 👇
- Generate Models. Push API.
- Create
Team
then createProject
withteamID
. - Delete
Project
. - ERROR
Expected behavior
The DataStore should correctly traverse the hasOne
relationship to delete the Team
record before deleting the Project
record.
Code Snippet
Schema
type Project @model {
id: ID!
name: String
teamID: ID!
team: Team @connection(fields: ["teamID"])
}
type Team @model {
id: ID!
name: String!
}
Create Team
then create Project
with teamID
.
DataStore.save(
new Team({ name: 'Team Name' })
)
.then(async ({ id: teamID }) => {
DataStore.save(
new Project({ teamID })
)
})
Delete Project
.
DataStore.delete(Project, project.id)
What is Configured? Amplify 4.27.1 Simple Auth with Cognito GraphQL / Dynamo API DataStore NextJS app (irrelevant)
Additional context A couple of notes.
- From the implementation, looks like an index is expected for the
hasOne
relationship. https://github.com/aws-amplify/amplify-js/blob/main/packages/datastore/src/storage/adapter/indexeddb.ts#L676-L682
case 'HAS_ONE':
for await (const model of models) {
const recordToDelete = <T>await this.db
.transaction(storeName, 'readwrite')
.objectStore(storeName)
.index(index) // <-- 🤔
.get(model.id);
- This goes counter to the docs where it states:
https://docs.amplify.aws/cli/graphql-transformer/directives#has-one A Has One
@connection
can only reference the primary index of a model (ie. it cannot specify a “keyName” as described below in the Has Many section).
- This is also where the error occurs, as
index
isundefined
.
Issue Analytics
- State:
- Created 3 years ago
- Comments:21 (8 by maintainers)
Top Results From Across the Web
DataStore - Relational models - JavaScript - AWS Amplify Docs
Learn more about how DataStore handles relationships between Models, such as "has one", "has many", "belongs to". - JavaScript - AWS Amplify Docs....
Read more >Lazy loading & nested query predicates for AWS Amplify ...
DataStore provides frontend app developers the ability to build ... You can also lazy load hasOne relationships, for example: await ...
Read more >Laravel - Delete with all relations
When a user with the id of 1 gets deleted. All orders where the user_id is equal to 1 should also be deleted,...
Read more >Model
Delete multiple instances, or set their deletedAt timestamp to the current time if paranoid is enabled. Params: Name, Type, Attribute, Description. options ...
Read more >Modeling Relationships in GraphQL | AppSync | Amplify | AWS
Your browser can't play this video. Learn more. Switch camera ... Modeling Relationships in GraphQL | AppSync | Amplify | AWS | Angular....
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 FreeTop 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
Top GitHub Comments
We’ve released a fix for this issue in aws-amplify@3.4.0.
For existing projects, you may need to set this CLI feature flag to
true
in./amplify/cli.json
. Afterward, make sure you have the latest version of @aws-amplify/cli, and runamplify codegen models
.If the model is getting generated correctly, you will see a new
targetName
property in the schema.js containing the field you’ve specified in@connection
.For example:
For this schema.graphql
You should see the following in
/models/schema.js
after you runamplify codegen models
:Ran into the same issue.
Any news on this @mauerbac ?