json-server icon indicating copy to clipboard operation
json-server copied to clipboard

DELETE request will delete all data

Open MiYogurt opened this issue 7 years ago • 18 comments

Version: 0.14.0

{
  "layers": [
    {
      "id": "7d-UXfgmm",
      "componentId": "aaa"
    },
    {
      "id": "HXf8PkKl7",
      "componentId": "aaa"
    },
    {
      "id": "kNJ083om1",
      "componentId": "aaa"
    },
    {
      "id": "DYPg3hl5s",
      "componentId": "aaa"
    }
  ],
  "components": [
    {
      "id": "K8iv0hJaQ",
      "label": "component"
    }
  ]
}
http DELETE :3000/layers/HXf8PkKl7

The will delete all layers 。

MiYogurt avatar Nov 03 '18 15:11 MiYogurt

Still not fixed...

Akiyamka avatar Sep 25 '19 14:09 Akiyamka

same here

ayxos avatar Nov 08 '19 07:11 ayxos

I'm having this problem as well.

faradayfan avatar Mar 20 '20 15:03 faradayfan

This bug still happens today!

Zhouqchao avatar Mar 31 '20 09:03 Zhouqchao

same here

yassotreyo avatar Apr 15 '20 06:04 yassotreyo

Almost 2 yrs, still not fixed :-)

XtheWiz avatar Jun 10 '20 14:06 XtheWiz

I'm having the some problem too .

Damoness avatar Jul 02 '20 07:07 Damoness

I think a simple framework implements a simple problem, why not use aok.js

zhangweiHello avatar Jul 09 '20 07:07 zhangweiHello

I'm having the same issues...multiple instances of the same element randomly deleted when DELETE request to one single element

aryan-curiel avatar Jul 19 '20 19:07 aryan-curiel

Hi! This is not a random error. I found that if there is some kind of connection between elements, then DELETE request will erase both of them: I have a "database":

{
    "users":[
        {"id":1, "name": "Bob", "email":"bob@gmailcom", "age": 35},
        {"id":2, "name": "Sue", "email":"sue@gmailcom", "age": 32},
        {"id":3, "name": "Cloe", "email":"cloe@gmailcom", "age": 19},
        {"id":4, "name": "Jane", "email":"jane@gmailcom", "age": 26}
    ],
    "comments":[
        {"id":1, "message1": "Here should be some comment", "userId": 2},
        {"id":2, "message2": "Here should be many comment", "userId": 4},
        {"id":3, "message3": "Here should be more comment", "userId": 1}
    ]
}

and I run this from console: let fetchOpt = { method: "delete", mode: "cors", cache: "no-cache" }; fetch("http://localhost:3000/users/4", fetchOpt);

then server will delete "Jane with id:4" from users, and "message2 with userId: 4" from comments

Try it. Then I changed "userId" to "usr" and there is no more "strange delete"

MicroRobi avatar Aug 09 '20 09:08 MicroRobi

Expected behaviour: https://github.com/typicode/json-server/blob/master/tests/server/plural-with-custom-foreign-key.js#L110

default value: https://github.com/typicode/json-server/blob/574cb817dc0bb2a67cd5ed38a0fc3211d4058804/src/server/router/index.js#L16

logic: https://github.com/typicode/json-server/blob/7ce599c50290e5c3a2336fd5b0bd84c3508df236/src/server/router/plural.js#L329

Akiyamka avatar Aug 11 '20 15:08 Akiyamka

I'm having this issue with version 0.16.3

vinicamposdev avatar Mar 29 '21 21:03 vinicamposdev

same issue

antonreshetov avatar Apr 04 '22 10:04 antonreshetov

I debugged the source code and found that the logic is: delete all data that are not related to each other (through xxId as the association flag).

When we delete data, it is equivalent to cleaning the entire db.json once, regardless of whether it is the currently operating table.

Here are some relevant test cases: plural.js#L628

image

This is a huge looking db.json file, However, when we run the http delete :3000/xxxs/1 command, we will find that a lot of data is deleted.

Before deleting a piece of data:

View Collapsed Data
{
  "weddings": [
    {
      "id": "xw",
      "name": "hello"
    },
    {
      "id": "xw2",
      "name": "hello"
    }
  ],
  "dels": [],
  "users": [
    {
      "id": "1",
      "readme": "This data will be deleted because it has delId and the dels table exists, but they have no dependencies",
      "delId": "about to be deleted",
      "ua": "chrome"
    }
  ],
  "bless": [
    {
      "id": "1",
      "name": "李莫愁",
      "content": "风雨同舟,任他沧海桑田",
      "userId": "a",
      "weddingId": "xw"
    },
    {
      "id": "2",
      "name": "宋静",
      "content": "Kxkben Egwv Ixwdflns Qbvec Jbngto Iidqyh",
      "userId": "b",
      "weddingId": "xw"
    },
    {
      "id": "3",
      "name": "林超",
      "content": "Vxoeft Vjh Bhp Exbgevz Xhgsyabf Ljindxdylk Kqiowkmrdb",
      "userId": "c",
      "xxxId": "deletedelete",
      "weddingId": "xw"
    }
  ],
  "xxxs": [
    {
      "id": "1",
      "img": "https://dummyimage.com/750x1334.jpg",
      "weddingId": "xw"
    },
    {
      "id": "2",
      "img": "https://dummyimage.com/750x1334.jpg",
      "weddingId": "xw"
    }
  ]
}
http delete :3000/xxxs/1

After deleting a piece of data, Too much data was cleaned up.:

{
  "weddings": [
    {
      "id": "xw",
      "name": "hello"
    },
    {
      "id": "xw2",
      "name": "hello"
    }
  ],
  "dels": [],
  "users": [],
  "bless": [],
  "xxxs": [
    {
      "id": "2",
      "img": "https://dummyimage.com/750x1334.jpg",
      "weddingId": "xw"
    }
  ]
}

So how do we avoid this (assuming the program has no bugs)?

  • Create data that maintains a reference relationship
  • Don't arbitrarily use key names like ...Id, unless they have a reference relationship, they may be deleted
  • Use avoidable options _noRemoveDependents

For example the following data will not be cleaned:

View Collapsed Data
{
  "weddings": [
    {
      "test": "一些数据",
      "id": "l2MIvPE"
    },
    {
      "test": "一些数据",
      "id": "G-v2Ert"
    },
    {
      "test": "一些数据",
      "id": "Bt2hQhG"
    },
    {
      "test": "一些数据",
      "id": "9sntAZw"
    },
    {
      "test": "一些数据",
      "id": "gJx_MAK"
    },
    {
      "test": "一些数据",
      "id": "VS3qmLL"
    },
    {
      "test": "一些数据",
      "id": "UMUkam_"
    },
    {
      "id": "18212341234",
      "text": "一些业的发展"
    }
  ],
  "users": [
    {
      "id": "1",
      "ua": "chrome"
    }
  ],
  "bless": [
    {
      "test": "一些数据",
      "weddingId": "UMUkam_",
      "id": 2
    },
    {
      "test": "一些数据",
      "weddingId": "UMUkam_",
      "id": 3
    },
    {
      "test": "一些数据",
      "weddingId": "UMUkam_",
      "id": 4
    },
    {
      "test": "一些数据",
      "weddingId": "UMUkam_",
      "id": 5
    },
    {
      "test": "一些数据",
      "weddingId": "UMUkam_",
      "id": 6
    },
    {
      "test": "一些数据",
      "weddingId": "UMUkam_",
      "id": 7
    },
    {
      "text": "一些业的发展",
      "weddingId": "18212341234",
      "id": 8
    },
    {
      "text": "一些业的发展",
      "weddingId": "18212341234",
      "id": 9
    },
    {
      "text": "一些业的发展",
      "weddingId": "18212341234",
      "id": 10
    },
    {
      "text": "一些业的发展",
      "weddingId": "18212341234",
      "id": 11
    },
    {
      "text": "一些业的发展",
      "weddingId": "18212341234",
      "id": 12
    },
    {
      "text": "一些业的发展",
      "weddingId": "18212341234",
      "id": 13
    },
    {
      "text": "一些业的发展",
      "weddingId": "18212341234",
      "id": 14
    },
    {
      "text": "一些业的发展",
      "weddingId": "18212341234",
      "id": 15
    }
  ],
  "photos": []
}

wll8 avatar Nov 09 '22 08:11 wll8

If you are trying to delete aome object with unexisting foreign key, the delete request will delete all data

loosq avatar Mar 03 '23 19:03 loosq

@loosq

This is the solution: https://github.com/wll8/json-server#options_noremovedependents

wll8 avatar Mar 04 '23 01:03 wll8

Run json-server with the flag --foreignKeySuffix SomeValue.

SomeValue can be whatever you want, it just needs to be different from the default Id and, of course, from whatever suffix you usually write when estabilishing a relationship between objects.

Overall, @wll8 solution is the best to handle this issue.

nicotakenotice avatar Jun 08 '23 08:06 nicotakenotice

how to delete all ?

xuananh2212 avatar Dec 15 '23 10:12 xuananh2212