Cannot delete model in AWS Amplify DataStore/ Appsync

0

I am writing an app with Amplify and I am unable to delete a Model called booking. Every booking has on client and one tutor relationship:

type Client @model @auth(rules: [{allow: groups, groups: ["admins"], operations: [read, create, update, delete]}, {allow: private}, {allow: public, operations: [read]}]) {
  id: ID!
  email: AWSEmail!
  firstName: String!
  lastName: String!
  authToken: String!
  phone: AWSPhone
  credits: Float!
  Students: [Student] @hasMany(indexName: "byClient", fields: ["id"])
}
type Tutor @model @auth(rules: [{allow: public, operations: [read, create, update]}, {allow: groups, groups: ["admins"], operations: [read, create, update, delete]}, {allow: groups, groups: ["tutors"], operations: [read, update, delete, create]}, {allow: private}]) {
  id: ID!
  email: AWSEmail!
  firstName: String!
  lastName: String!
  authToken: String!
  phone: AWSPhone
  isRemote: Boolean!
  isInPerson: Boolean!
  hourlyRate: Float!
  grade: String!
  isSearchable: Boolean!
  isActive: Boolean!
  location: String
  resumeUrl: String
  transcriptUrl: String
  photoUrl: String
  bio: String
  subjects: [String]
  schoolAttended: String
}
type Booking @model @auth(rules: [{allow: private}]) {
  id: ID!
  startDateTime: AWSDateTime!
  duration: Int!
  Tutor: Tutor! @hasOne
  bookingCode: String!
  amount: Float!
  discount: Float!
  clientNotes: String
  tutorNotes: String
  tutorPaid: Boolean!
  clientPaid: Boolean!
  resources: String
  Client: Client @hasOne
  studentName: String
  tutorRating: Int
  clientRating: Int
  bookingType: String
  tutorPayAmount: Float
  tip: Float
  transactionId: String
  tutorMileage: Float
  meetingUrl: AWSURL
  hwFiles: [String]
  resourceFiles: [String]
  meetingInstructions: String
  sessionTaught: Boolean
}

When I try to delete with App sync:

const bookingDetails: DeleteBookingInput = {
                id: selectedBookingId,
              };
              const deletedBooking = await API.graphql<
                GraphQLQuery<DeleteBookingMutation>
              >({
                query: mutations.deleteBooking,
                variables: { input: bookingDetails },
              });

I get this error: AppSync Error while deleting

Then when I try to delete using DataStore,

const b = await DataStore.delete(Booking, selectedBookingId);

I get this error:

Error: Namespace Resolver for 'Object' not found! This is probably a bug in '@amplify-js/datastore'. at IndexedDBAdapter.namespaceResolver (http://localhost:3000/static/js/bundle.js:84021:11) at IndexedDBAdapter.getIndexKeyValuesFromModel (http://localhost:3000/static/js/bundle.js:88981:30) at IndexedDBAdapter.<anonymous> (http://localhost:3000/static/js/bundle.js:90170:30) at step (http://localhost:3000/static/js/bundle.js:229816:17) at Object.next (http://localhost:3000/static/js/bundle.js:229765:14) at fulfilled (http://localhost:3000/static/js/bundle.js:229724:24)
message
: 
"Namespace Resolver for 'Object' not found! This is probably a bug in '@amplify-js/datastore'."
stack
: 

I am able to delete the booking usually from the Amplify console, but even that has failed occasionally. How do I work around this?

1 Answer
0

Based on your schema, you have a one-to-one relationship between the Booking, Client, and Tutor models. When you try to delete a Booking, it's possible that it fails due to the relationships with the Client and Tutor models.

To resolve this issue, you can modify your schema to use the @connection directive instead of @hasOne. This will help you establish a one-to-one relationship between the models without causing errors when deleting related items.

Update your schema as follows:

type Client @model @auth(rules: [{allow: groups, groups: ["admins"], operations: [read, create, update, delete]}, {allow: private}, {allow: public, operations: [read]}]) {
  id: ID!
  email: AWSEmail!
  firstName: String!
  lastName: String!
  authToken: String!
  phone: AWSPhone
  credits: Float!
  Students: [Student] @hasMany(indexName: "byClient", fields: ["id"])
  booking: Booking @connection
}

type Tutor @model @auth(rules: [{allow: public, operations: [read, create, update]}, {allow: groups, groups: ["admins"], operations: [read, create, update, delete]}, {allow: groups, groups: ["tutors"], operations: [read, update, delete, create]}, {allow: private}]) {
  id: ID!
  email: AWSEmail!
  firstName: String!
  lastName: String!
  authToken: String!
  phone: AWSPhone
  isRemote: Boolean!
  isInPerson: Boolean!
  hourlyRate: Float!
  grade: String!
  isSearchable: Boolean!
  isActive: Boolean!
  location: String
  resumeUrl: String
  transcriptUrl: String
  photoUrl: String
  bio: String
  subjects: [String]
  schoolAttended: String
  booking: Booking @connection
}

type Booking @model @auth(rules: [{allow: private}]) {
  id: ID!
  startDateTime: AWSDateTime!
  duration: Int!
  bookingCode: String!
  amount: Float!
  discount: Float!
  clientNotes: String
  tutorNotes: String
  tutorPaid: Boolean!
  clientPaid: Boolean!
  resources: String
  studentName: String
  tutorRating: Int
  clientRating: Int
  bookingType: String
  tutorPayAmount: Float
  tip: Float
  transactionId: String
  tutorMileage: Float
  meetingUrl: AWSURL
  hwFiles: [String]
  resourceFiles: [String]
  meetingInstructions: String
  sessionTaught: Boolean!
  clientId: ID! @hasOne
  tutorId: ID! @hasOne
}

Based on your schema, you have a one-to-one relationship between the Booking, Client, and Tutor models. When you try to delete a Booking, it's possible that it fails due to the relationships with the Client and Tutor models.

To resolve this issue, you can modify your schema to use the @connection directive instead of @hasOne. This will help you establish a one-to-one relationship between the models without causing errors when deleting related items.

Update your schema as follows:

graphql Copy code type Client @model @auth(rules: [{allow: groups, groups: ["admins"], operations: [read, create, update, delete]}, {allow: private}, {allow: public, operations: [read]}]) { id: ID! email: AWSEmail! firstName: String! lastName: String! authToken: String! phone: AWSPhone credits: Float! Students: [Student] @hasMany(indexName: "byClient", fields: ["id"]) booking: Booking @connection }

type Tutor @model @auth(rules: [{allow: public, operations: [read, create, update]}, {allow: groups, groups: ["admins"], operations: [read, create, update, delete]}, {allow: groups, groups: ["tutors"], operations: [read, update, delete, create]}, {allow: private}]) { id: ID! email: AWSEmail! firstName: String! lastName: String! authToken: String! phone: AWSPhone isRemote: Boolean! isInPerson: Boolean! hourlyRate: Float! grade: String! isSearchable: Boolean! isActive: Boolean! location: String resumeUrl: String transcriptUrl: String photoUrl: String bio: String subjects: [String] schoolAttended: String booking: Booking @connection }

type Booking @model @auth(rules: [{allow: private}]) { id: ID! startDateTime: AWSDateTime! duration: Int! bookingCode: String! amount: Float! discount: Float! clientNotes: String tutorNotes: String tutorPaid: Boolean! clientPaid: Boolean! resources: String studentName: String tutorRating: Int clientRating: Int bookingType: String tutorPayAmount: Float tip: Float transactionId: String tutorMileage: Float meetingUrl: AWSURL hwFiles: [String] resourceFiles: [String] meetingInstructions: String sessionTaught: Boolean! clientId: ID! @hasOne tutorId: ID! @hasOne } After updating your schema, you need to run amplify push to apply the changes to your backend.

Now, when you delete a Booking, it should no longer fail due to the relationships with the Client and Tutor models. Make sure to update your queries and mutations in your code to reflect the new schema changes.

profile picture
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions