aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

DataStore throws error when trying to delete a Model with a relation #8564

Closed PeteDuncanson closed 3 years ago

PeteDuncanson commented 3 years ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

DataStore

Amplify Categories

Not applicable

Environment information

``` System: OS: Windows 10 10.0.19041 CPU: (16) x64 Intel(R) Core(TM) i7-10875H CPU @ 2.30GHz Memory: 15.74 GB / 31.84 GB Binaries: Node: 14.16.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD npm: 6.14.11 - C:\Program Files\nodejs\npm.CMD Browsers: Chrome: 91.0.4472.124 Edge: Spartan (44.19041.1023.0), Chromium (91.0.864.64) Internet Explorer: 11.0.19041.1 npmPackages: @asseinfo/react-kanban: ^2.1.0 => 2.1.0 @aws-amplify/ui-react: ^0.2.38 => 0.2.38 @brainhubeu/react-file-input: 0.7.21 => 0.7.21 @headlessui/react: ^1.2.0 => 1.2.0 @ideal-postcodes/api-typings: ^2.0.1 => 2.0.1 @ideal-postcodes/core-browser: ^1.5.2 => 1.5.2 @sentry/react: ^6.3.0 => 6.3.0 @sentry/tracing: ^6.2.4 => 6.2.4 @tailwindcss/aspect-ratio: ^0.2.0 => 0.2.0 @tailwindcss/forms: ^0.2.1 => 0.2.1 @tailwindcss/typography: ^0.4.0 => 0.4.0 (0.2.0) @tailwindcss/ui: ^0.6.2 => 0.6.2 @tailwindui/react: ^0.1.1 => 0.1.1 @testing-library/jest-dom: ^4.2.4 => 4.2.4 @testing-library/react: ^9.3.2 => 9.5.0 @testing-library/user-event: ^7.1.2 => 7.2.1 @types/javascript-time-ago: ^2.0.2 => 2.0.2 @types/jest: ^26.0.24 => 26.0.24 @types/node: ^14.14.33 => 14.14.33 (13.9.2, 14.11.2) @types/reach__router: ^1.3.7 => 1.3.7 @types/react: ^16.14.5 => 16.14.5 (16.9.49) @types/react-dom: ^16.9.11 => 16.9.11 (16.9.8) @types/yup: ^0.29.11 => 0.29.11 @typescript-eslint/eslint-plugin: ^4.17.0 => 4.17.0 (4.2.0) @typescript-eslint/parser: ^4.17.0 => 4.17.0 (4.2.0) apollo-boost: ^0.4.9 => 0.4.9 autoprefixer: ^10.2.5 => 10.2.5 (7.2.6, 6.7.7, 9.7.4) aws: ^0.0.3-2 => 0.0.3-2 aws-amplify: ^3.3.23 => 3.3.23 aws-amplify-react: ^4.2.27 => 4.2.27 aws-sdk: ^2.860.0 => 2.860.0 axios: ^0.21.1 => 0.21.1 (0.19.0) chart.js: ^3.2.0 => 3.2.0 chart.js-auto: undefined () chart.js-helpers: undefined () eslint: ^7.21.0 => 7.21.0 (6.8.0) file-saver: ^2.0.5 => 2.0.5 formik: ^2.2.6 => 2.2.6 graphiql: ^1.4.0 => 1.4.0 graphql: ^15.5.1 => 15.5.0 (14.0.0) graphql-tag: ^2.12.5 => 2.11.0 history: 5 => 5.0.0 (4.10.1) postcode: ^5.1.0 => 5.1.0 postcss: ^8.2.8 => 8.2.8 (6.0.23, 7.0.27, 5.2.18, 8.2.4, 7.0.21) react: ^16.14.0 => 16.14.0 react-apollo: ^3.1.5 => 3.1.5 react-chartjs-2: ^3.0.2 => 3.0.2 react-datepicker: ^3.4.1 => 3.4.1 react-dom: ^16.14.0 => 16.14.0 react-icons: ^4.2.0 => 4.2.0 react-pagination-hook: ^0.0.1 => 0.0.1 react-router-dom: ^6.0.0-beta.0 => 6.0.0-beta.0 (4.3.1) react-scripts: 3.4.3 => 3.4.3 react-tag-autocomplete: ^6.1.0 => 6.1.0 react-three-state-checkbox: ^1.3.4 => 1.3.4 react-time-ago: ^6.2.2 => 6.2.2 react-use: ^15.3.8 => 15.3.8 rimraf: ^3.0.2 => 3.0.2 (2.7.1, 2.6.3) tailwind: ^4.0.0 => 4.0.0 tailwindcss: ^2.0.3 => 2.0.3 typescript: ^4.2.3 => 4.2.3 vest: ^2.2.3 => 2.2.3 yup: ^0.29.3 => 0.29.3 npmGlobalPackages: @aws-amplify/cli: 5.1.0 nodemon: 2.0.7 ts-node: 9.1.1 yarn: 1.22.10 ```

Describe the bug

I have a schema with several relations within it. I cannot seem to delete any of the models with a relation as I get the following Error:

Failed to execute 'index' on 'IDBObjectStore': The specified index was not found.

My Schema (trimmed as best I can) is below, the Gist is we have a Round (like a delivery service) which a StaffMember gets assigned too as they are the ones doing the Round. StaffMembers can be on multiple Rounds, so that should be a hasOne relationship from the Round to the StaffMember (this follows on from the example of a Comment hasOne BlogPost that it refers to in all the docs, https://docs.amplify.aws/lib/datastore/relational/q/platform/js#saving-relations). Each StaffMember also has a link to a User that we use for Authentication. StaffMember and User used to have a connection but I removed it in trying to fix this (wondered if it had issues with multiple nested relations) without any luck.

"""
Each user logged into the system should have an entry here
"""
type User
  @model
  @key(name: "byEmail", fields: ["email"], queryField: "userByEmail")
  @auth(
    rules: [
      { allow: groups, groups: ["admin"] }
      { allow: public, provider: apiKey }
    ]
  ) {
  """
  This is populated from the unqiue "sub" field on the user in cognito
  """
  id: ID!
  name: String
  username: String
  email: String!

  """
  A link to a staff member if this user is staff! Could be empty in which case they aren't staff :)
  """
  staffID: ID

  """
  Flag to let us know if it's a user or staff account
  """
  accountType: String
}

type StaffMember
  @model
  @key(name: "byEmail", fields: ["email"], queryField: "staffByEmail")
  @auth(
    rules: [
      { allow: groups, groups: ["admin"] }
      { allow: public, provider: apiKey }
    ]
  ) {
  id: ID!
  firstName: String!
  lastName: String!
  email: String
  phoneNumber: String
  roles: [String]
  userID: ID
}

type Round
  @model
  @auth(
    rules: [
      { allow: groups, groups: ["admin"] }
      { allow: public, provider: apiKey }
    ]
  ) {
  id: ID!
  date: AWSDateTime
  visits: [ID]
  technicianID: ID
  technician: StaffMember @connection(fields: ["technicianID"]) #This seems to be the cause!
  status: String
  areaID: ID!
  routingID: String
  roundDirection: String # RoundDirection
  notes: String
}

This seems to be being caused in this method (https://github.com/aws-amplify/amplify-js/blob/d16a8fbc5862281121812b1f8fc7af8bb001190d/packages/datastore/src/storage/adapter/AsyncStorageAdapter.ts#L492) when trying to delete, its trying to delete the StaffMember I have in the connection on Round. That causes it to blow up. If I change my schema to remove the connection to the StaffMember on Round then it will work. But I build the whole schema with GraphQL in mind so nearly everything has connections on it. Now when using DataStore it keeps blowing up when deleting. Even trying to go to AppSync directly with a GraphQL query still causes the error (when the connection is in place on Round to StaffMember) as I guess the subscription from AppSync causes it to try to delete it locally so its dying.

I've tried the fix mentioned in here https://github.com/aws-amplify/amplify-js/issues/6560#issuecomment-834618097 to enable a feature toggle in the /amplify/cli.json file (which didn't exist so I had to copy if from https://docs.amplify.aws/cli/reference/feature-flags#configuration). This injected in the new "targetName" fields so it was working as mentioned in the above issue.

This seems like quite a core thing, DataStore should be able to delete relational content right? Which makes me think this is either a mess up with my setup (why didn't I have a cli.json file for instance already? Its meant to already be there from what I've read) or my schema. This project is over a year old now on Amplify and a lot has changed with Amplify in that time, I wonder if something is busted that I need to update or something?

Happy to share my full schema but the above should give you an idea. Related content simply can't be deleted at all currently and I've no idea why.

Expected behavior

Running await DateStore.delete( myRound ) should delete the Round but not the related StaffMember (a StaffMember can have multiple rounds.

As shown in the docs https://docs.amplify.aws/lib/datastore/relational/q/platform/js#deleting-relations

Reproduction steps

Happy to privately share my app code if it will help. I've tried to make a demo repos but its slow going.

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

nickarocho commented 3 years ago

Hi @PeteDuncanson thank you for reaching out with such a detailed description of your problem. I'm sorry to hear you are having issues with deleting relational content via Datastore.

I'm just beginning to dive into the details you've provided to try and gain further insight to the root cause of the issue. At first glance, I noticed you are utilizing an older version of the aws-amplify library (looks like version 3.3.23 from your output provided above).

Not saying this will solve the problem, but I'm just curious if you are intentionally holding back from upgrading to the latest major version of the core library (as of today the latest version is 4.1.3) for a certain reason?

Thanks for providing some historical context while we look to reproduce the problem and hopefully help you get to the bottom of it!

PeteDuncanson commented 3 years ago

@nickarocho good question. I didn't know there was an update. I keep up to date with the CLI releases everytime they notify me about an update (which are really often, you lot of busy). I had no idea there was a separate thing for core! Thought it was all one and the same thing so I kind of thought I was on the cutting edge!

I can have a go at upgrading it and see if that changes anything and report back.

PeteDuncanson commented 3 years ago

Well. That was interesting.

The good news, that seems to have fixed it!

The interesting bit is yarn upgrade aws-amplify wouldn't upgrade it, kept it at v3.3.23 which might explain why I was on such an old version. Had to run yarn upgrade aws-amplify@^4.1.3 to force it.

Then gave my app another go and it deleted (via DataStore) without any errors! Woohoo!

I wonder how many other quirks that might have fixed :)

Thank you so much for taking a look and pointing me in the right direction, saved my weekend!

nickarocho commented 3 years ago

@PeteDuncanson yes the core JS library maintains its own release cycle, which can definitely be tricky to keep track of.

You can update all Amplify packages to the latest version using the following script (depending on if you use yarn or npm):

# Using YARN
yarn upgrade --latest --pattern aws-amplify

or

# Using NPM
npx npm-check-updates -i '/@?aws-amplify/' && npm update

Please let us know how this affects your app either positively or if there are any other side-effects due to the major version bump.

If you are still having an issue with interacting with relational content it might be related to your setup, in which case I'll be happy to help by creating a similar setup to your current environment.

Hope that helps!

nickarocho commented 3 years ago

You beat me to the punch!

Great, I'm so happy updating the core library solved your problem. For future reference, here is our documentation on updating all Amplify libraries.

Hope this provides a nice start to your weekend :) Thanks again for reaching out and never hesitate to contact us again if you run into any roadblocks, we are here to happily assist.

Cheers, closing this ticket but feel free to reopen if anything else similar comes up in your future development.

PeteDuncanson commented 3 years ago

@nickarocho not sure how you could surface that better, the way the CLI does it is wonderful and I keep up todate when I see there is a release, wonder if it could do something similar for the core etc? Just an idea.

Thanks again, have a great weekend!

nickarocho commented 3 years ago

@PeteDuncanson yes this is excellent feedback which I will be sharing with the team to improve the overall experience for everyone who uses Amplify across every platform.

Thanks again!

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.