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

DELETE call deletes all the entries. #13

Closed
injulkarnilesh opened this issue Jun 11, 2014 · 4 comments
Closed

DELETE call deletes all the entries. #13

injulkarnilesh opened this issue Jun 11, 2014 · 4 comments

Comments

@injulkarnilesh
Copy link

I am providing json file as a db source containing post elements as in : https://gist.github.com/injulkarnilesh/4cfd42501bfd5c13885f
Rest of the calls work fine
GET /posts/
GET /posts/3
POST /posts/
These calls work as expected.

But on
DELETE /posts/3
Response says
Response does not contain any data.
with status 204.
and json data in the file gets emptied to
{
"posts": []
}

Shouldn't it delete only the mentioned post rather than all the posts?

@typicode
Copy link
Owner

Actually, JSON-Server deletes dependent resources. For example, if you delete a post, it will delete comments that are associated with the post.

To do so, JSON-Server will look at the database and remove entries that references other entries that don't exist (based on the parentId property).

This is why all your posts got deleted. They are referencing users that don't exist.

To fix it, you can either remove the ̀userId` property or add an users table like this:

{
  "users": [
    { "id": 1 },
    { "id": 2 }
  ],
  "posts": {
    // ...
  }
}

Tell me if it solvers your problem?

@injulkarnilesh
Copy link
Author

Worked like a charm.
Thanks a lot.

@aboudard
Copy link

aboudard commented May 5, 2022

Hello, just a quick note for anyone having such issues :
If you ever have an Entity post that uses an id field because json-server works by default with this name, AND you also have another postId field (reflecting you own future database), you will encounter the same "delete all" behavior. All you need to do is align the values of these 2 fields.

{
    "id": 1,
    "postId": "PI524"
}

will be deleted every time, instead, align values :

{
    "id": "PI524",
    "postId": "PI524"
}

@Mahmoudibrahim03
Copy link

Hello, just a quick note for anyone having such issues : If you ever have an Entity post that uses an id field because json-server works by default with this name, AND you also have another postId field (reflecting you own future database), you will encounter the same "delete all" behavior. All you need to do is align the values of these 2 fields.

{
    "id": 1,
    "postId": "PI524"
}

will be deleted every time, instead, align values :

{
    "id": "PI524",
    "postId": "PI524"
}

image

what if I need to relate items like (questions - answers ) with form ID and each one should have its own id also @aboudard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants