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

Primary key is not "id" #101

Closed
ryderlee opened this issue Apr 27, 2017 · 8 comments
Closed

Primary key is not "id" #101

ryderlee opened this issue Apr 27, 2017 · 8 comments

Comments

@ryderlee
Copy link

I tried to declare the user table as:

table.increments('user_id').primary();
table.string('username');

but when I do a HTTP GET call to the server /user/1
I got the error

{
  "name": "BadRequest",
  "message": "select `user`.* from `user` where `id` = '1' - ER_BAD_FIELD_ERROR: Unknown column 'id' in 'where clause'",
  "code": 400,
  "className": "bad-request",
  "errors": {}
}

Expected result

the user record would be shown accordingly.

@ghost
Copy link

ghost commented Apr 27, 2017

On your example, you've basically named the primary key "user_id". If you want a primary key with the "id" field name :

table.increments( 'id' ).primary();

@ryderlee
Copy link
Author

and I would like to name the primary key as "user_id". The problem is, when I make a HTTP GET call to /user/1 I got the above error.
Any idea?

@daffl
Copy link
Member

daffl commented Apr 27, 2017

If you want to use an id key that is not id you have to tell the adapter which id property to use when initializing the service as documented here.

app.use('/messages', service({
  id: 'user_id',
  Model: db,
  name: 'messages'
}));

@ryderlee
Copy link
Author

well it works in find but not under get, so if I am calling /user/ it works, but not /user/1 and the error is :

{
  "name": "NotFound",
  "message": "No record found for id '1'",
  "code": 404,
  "className": "not-found",
  "errors": {}
}

Any idea?

@daffl
Copy link
Member

daffl commented Apr 28, 2017

Are you sure? This functionality is being explicitly tested here.

@daffl
Copy link
Member

daffl commented May 3, 2017

I'm going to close this since setting a different id should be supported as mentioned in the comments and we don't have enough additional information to reproduce the issue.

@daffl daffl closed this as completed May 3, 2017
@ryderlee
Copy link
Author

ryderlee commented May 5, 2017

Sorry but I still not able to fix it. I would like to highlight the endpoint that I call is to /user/1 (that means to get the user with user_id = 1) the error I got is :

info: error: user - Method: get: No record found for id '1'
error:  Error

again the id property has been defined and I can do a get by calling /user and the result is good.

@daffl
Copy link
Member

daffl commented May 9, 2017

Can you share a repository to reproduce the issue?

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

2 participants