Skip to content
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

DataStore Delete - Failed to execute 'index' on 'IDBObjectStore' #7049

Closed
myfairshare opened this issue Oct 23, 2020 · 7 comments
Closed

DataStore Delete - Failed to execute 'index' on 'IDBObjectStore' #7049

myfairshare opened this issue Oct 23, 2020 · 7 comments
Labels
DataStore Related to DataStore category

Comments

@myfairshare
Copy link

Describe the bug
When I try to delete an item in DataStore which belongs to a 1 to many relationship I get the 'failed to execute 'index' on 'IDBObjectStore' error. I noticed in another issue DataStore has a problem with deleting such a relationship here..
##5088

Expected behavior
I expect the the user to be able to delete a rental unit

Code Snippet
My schema looks like this..a user has many rental units

type User
  @model
  @auth(rules: [{ allow: owner, ownerField: "id" }]) {
  id: ID!
  userType: UserType!
  username: String!
  email: String!
  rentalUnits: [RentalUnit] @connection(keyName: "OwnerUnits", fields: ["id"])
}

type RentalUnit
  @model
  @key(name: "OwnerUnits", fields: ["ownerID"])
  @auth(rules: [{ allow: owner, ownerField: "ownerID" }]) {
  id: ID!
  ownerID: ID!
  unitType: UnitType!
}

Thank you for any suggestions.

@myfairshare myfairshare added the to-be-reproduced Used in order for Amplify to reproduce said issue label Oct 23, 2020
@amhinson amhinson added the DataStore Related to DataStore category label Oct 23, 2020
wei added a commit to wei/amplify-js-7049 that referenced this issue Oct 28, 2020
@wei
Copy link
Contributor

wei commented Oct 28, 2020

Hi @myfairshare I have tried to reproduce your issue. However, it is working fine for me. Code: wei/amplify-js-7049

Feel free to try it and let me know if it works for you. If possible, please create a minimal reproducible example repo from scratch with amplify CLI 4.33.1 or on top of my repo so we can further assist you! Thank you!

@iartemiev
Copy link
Member

@myfairshare based on the auth rule on your User model, it appears you're attempting to store the owner information in the id field. This is not supported. The id field is managed by DataStore and must not be mutated.

Please try changing your schema to:

type User
  @model
  @auth(rules: [{ allow: owner, ownerField: "userId" }]) {
  id: ID!
  userId: String
  userType: UserType!
  username: String!
  email: String!
  rentalUnits: [RentalUnit] @connection(keyName: "OwnerUnits", fields: ["userId"])
}

Also, could you share the code for how you're saving and deleting records with these models?

@myfairshare
Copy link
Author

Thanks wei and iartemiev,

Thanks for your help, will be going back to this soon and will report how I things are going, thanks again!

@myfairshare
Copy link
Author

Hi Iartemiev,

Have come back to this today and followed your suggestions with a slight change and have got delete working.

I added the userId but was getting an error generating schema it would only take type ID! non nullable.
For the userId value I used the users cognito sub id..

const { attributes } = await Auth.currentAuthenticatedUser();
const {sub} = attributes

Then..

const user = await DataStore.save(
        new User({
          username,
          email,
          userType: 'landlord',
          userId: sub,
....
        })
      )

Here's my delete unit function..

async function deleteUnit() {
    const toDeleteId = id
    try {
      const toDelete = await DataStore.query(RentalUnit, toDeleteId)
      DataStore.delete(toDelete)
      history.push('/myunits')
      snackDispatch({
        type: 'SET_SUCCESS_MSG',
        open: true,
        severity: 'success',
        message: 'Property has been deleted.',
      })
    } catch (err) {
      console.log('error deleting unit', err)
      snackDispatch({
        type: 'SET_ERROR_MSG',
        open: true,
        severity: 'error',
        message: 'Error deleting property property.',
      })
    }
  }

I'm pretty much following the DataStore amplify docs as best I can, thanks again both of you for your help.

@iartemiev
Copy link
Member

@myfairshare it sounds like you were able to get this working then? If so, can we close this issue?

@myfairshare
Copy link
Author

Yes please, thanks again!

@iartemiev iartemiev removed the to-be-reproduced Used in order for Amplify to reproduce said issue label Nov 3, 2020
@github-actions
Copy link

github-actions bot commented Nov 4, 2021

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
DataStore Related to DataStore category
Projects
None yet
Development

No branches or pull requests

4 participants