Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implicit foreign key mapping not working #1567

Open
Tzal3x opened this issue Jul 4, 2024 · 1 comment
Open

Implicit foreign key mapping not working #1567

Tzal3x opened this issue Jul 4, 2024 · 1 comment

Comments

@Tzal3x
Copy link

Tzal3x commented Jul 4, 2024

It should be possible to be able to find the entries of another entity by using the foreign key.

For example let's say we have two entities: users and companies.

Each users entry has a companyId.

The json server should be able to fetch all the users of a company.

i.e.

Given the following db.json :

{
  "users": [
    { "id": "23", "firstName": "Bill", "age": 20, "companyId": "1" },
    { "id": "40", "firstName": "Alex", "age": 40, "companyId": "2" },
    { "id": "41", "firstName": "Nick", "age": 40, "companyId": "2" }
  ],
  "companies": [
    { "id": "1", "name": "Apple", "description": "iphone" },
    { "id": "2", "name": "Google", "description": "search" }
  ]
}

Running the server with npx json-server db.json

Expected behavior

curl http://localhost:3000/companies/2/users
[
    { "id": "40", "firstName": "Alex", "age": 40, "companyId": "2" },
    { "id": "41", "firstName": "Nick", "age": 40, "companyId": "2" }
]

Actual behavior

$ curl http://localhost:3000/companies/2/users
Not Found
@d4n1b
Copy link

d4n1b commented Oct 13, 2024

Funny. I'm using json-server v0.17.4 with node v20 and works fine with the data you provided?

image

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

No branches or pull requests

2 participants