Open Jack-Huskinson opened 2 years ago
Hi @Jack-Huskinson :wave: thanks for raising this issue.
When deleting the parent in a Has One relationship the current expected behavior, for our JS implementation of DataStore, is for the child to get deleted as well, similar to Many-to-many relationships. We will correct our documentation to reflect this. Apologies for the inconvenience.
The team is working towards making cascading deletes an opt-in feature in the future.
Hi @chrisbonifacio, if I don't want to delete the child. What should I do but keep the relationship?
Hi, has there been a resolution to this? Im facing the same issue
Hi, has there been a resolution to this? Im facing the same issue
+1
I think there is a bug on reverse also. When I am deleting child, parent is also getting deleted.
We are also facing this issue. Deleting user data also deletes linked model data which is definitely not desirable.
For example: deleting a user review of content also deletes the content.
I have just amended my tables and logic to update entry to INACTIVE instead of deleting and querying only the ACTIVE entries
On Wed, Apr 5, 2023 at 10:06 PM Shawn Nystrand @.***> wrote:
We are also facing this issue. Deleting user data also deletes linked model data which is definitely not desirable.
For example: deleting a user review of content also deletes the content.
— Reply to this email directly, view it on GitHub https://github.com/aws-amplify/amplify-js/issues/9553#issuecomment-1497551617, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABEGB6XH4ITDWMVHPP3VVQDW7V4ABANCNFSM5NR2QCYA . You are receiving this because you commented.Message ID: @.***>
Hi All, did someone found any fix for this, I am facing same issue. Deleting OrderedDish data, Dish data also gets deleted.
Hi everyone, can you share what version of aws-amplify
and/or @aws-amplify/datastore
you are using as well as the schemas and code snippets that reproduce this behavior?
Just want to be sure if I am testing the correct relationships between models.
Hi @chrisbonifacio, thanks for looking into this issue. Please find my response below. aws-amplify version -- 5.0.22 Below I have attached schema models..
type OrderDish @model @auth(rules: [{allow: public}]) { id: ID! quantity: Int! Dish: Dish @hasOne orderID: ID! @index(name: "byOrder") } type Order @model @auth(rules: [{allow: public}]) { id: ID! userID: ID! @index(name: "byUser") Restaurant: Restaurant @hasOne total: Float! OrderDishes: [OrderDish] @hasMany(indexName: "byOrder", fields: ["id"]) status: OrderStatus! } type BasketDish @model @auth(rules: [{allow: public}]) { id: ID! quantity: Int! Dish: Dish @hasOne basketID: ID! @index(name: "byBasket") }
type Basket @model @auth(rules: [{allow: public}]) { id: ID! BasketDishes: [BasketDish] @hasMany(indexName: "byBasket", fields: ["id"]) userID: ID! @index(name: "byUser") restaurantID: ID! @index(name: "byRestaurant") } type Dish @model @auth(rules: [{allow: public}]) { id: ID! name: String! description: String price: Float! image: String restaurantID: ID! @index(name: "byRestaurant") } When I am trying to delete any Basket which contains all BasketDishes, Dish table also gets deleted.. Please let me know if I am doing something wrong here.
Cascading delete is the intended behavior of DataStore. There is currently not an option to disabled this functionality.
You can inverse the direction of the relationship if you would like the Dish
to persist.
type FlippedOrder @model @auth(rules: [{allow: public}]) {
id: ID!
dish: FlippedDish @belongsTo
}
type FlippedDish @model @auth(rules: [{allow: public}]) {
id: ID!
orders: [FlippedOrder] @hasMany
}
Deleting a FlippedOrder
will not delete the FlippedDish
.
const flippedDish = await DataStore.save(new FlippedDish({}));
const flippedOrder = await DataStore.save(new FlippedOrder({ dish: flippedDish }));
...
await DataStore.delete(FlippedOrder, flippedOrder.id);
// flippedDish is not deleted
const dish = await DataStore.query(FlippedDish, flippedDish.id);
any one solve this let me now i have the same issue when i delete the basket my dishes inside that was also deleted
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
DataStore
Amplify Categories
No response
Environment information
Describe the bug
When using DataStore.delete(), the instance I am passing will be deleted as expected, however other items in different data models will also be deleted if they are related to the instance.
For example, in my project I have a model for a product and a model for a cart product:
When I try to do
DataStore.delete(CartProduct, '123');
it will delete the CartProduct with the ID '123' as expected, but it will also delete the Product that is connected to the CartProduct, along with the Category that is linked to the Product.Expected behavior
Only the instance being passed should be deleted, not the realted instances.
Reproduction steps
amplify add api
=> 'graphql'DataStore.delete(CartProduct, ID)
Code Snippet
Log output
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
I would also like to note that my models are defined in a React Native project and I am using
amplify pull
to use multiple frontends. In the React Native project I never had this issue.