Code Monkey home page Code Monkey logo

Comments (4)

typicode avatar typicode commented on May 22, 2024 4

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?

from json-server.

aboudard avatar aboudard commented on May 22, 2024 1

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"
}

from json-server.

injulkarnilesh avatar injulkarnilesh commented on May 22, 2024

Worked like a charm.
Thanks a lot.

from json-server.

Mahmoudibrahim03 avatar Mahmoudibrahim03 commented on May 22, 2024

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

from json-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.