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.

DataStore cannot delete `hasOne` relationships

See original GitHub issue

Describe 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

  1. Init Amplify. Add GQL API. Enable DataStore.
  2. Define the Schema below 👇
  3. Generate Models. Push API.
  4. Create Team then create Project with teamID.
  5. Delete Project.
  6. 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.

  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 is undefined.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:21 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
iartemievcommented, May 7, 2021

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 run amplify 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

type Project @model {
  id: ID!
  name: String
  teamID: ID!
  team: Team @connection(fields: ["teamID"])
}

type Team @model {
  id: ID!
  name: String!
}

You should see the following in /models/schema.js after you run amplify codegen models:

export const schema = {
  "models": {
    "Project": {
      "name": "Project",
        "fields": {
          ...,
          "teamID": {
            "name": "teamID",
            "isArray": false,
            "type": "ID",
            "isRequired": true,
            "attributes": []
          },
          "team": {
            "name": "team",
            "isArray": false,
            "type": {
              "model": "Team"
            },
            "isRequired": false,
            "attributes": [],
            "association": {
              "connectionType": "HAS_ONE",
              "associatedWith": "id",
              "targetName": "teamID" <----
            }
          }
          ...,
2reactions
TheMoumscommented, Sep 11, 2020

Ran into the same issue.

Any news on this @mauerbac ?

Read more comments on GitHub >

github_iconTop 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 >

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