Model relation call fails when id is 0
Bug/Feature request
When creating a relation (tested with hasMany and belongsTo relations) between two models it will crash the application if you try to get the related items from a model with id = 0. These are possible due to them coming from a database where ids can be 0.
Expected result
Imagine i have a modelA with a hasMany relation to modelB called "child". When i try to use the relation using: http://localhost:3000/api/modelA/1/child it will work but when i want to get the child elements of the modelA item with the id 0 like http://localhost:3000/api/modelA/0/child it will cause a 500 error saying
Error: must specify id or data
at Function.ModelCtor.sharedCtor (<Workspace>\TestLoopback\loopback-test\node_modules\loopback\lib\model.js:185:12)
at SharedMethod.invoke (<Workspace>\TestLoopback\loopback-test\node_modules\strong-remoting\lib\shared-method.js:262:25)
at HttpContext.invoke (<Workspace>\TestLoopback\loopback-test\node_modules\strong-remoting\lib\http-context.js:295:12)
at restPrototypeMethodHandler (<Workspace>\TestLoopback\loopback-test\node_modules\strong-remoting\lib\rest-adapter.js:411:9)
at Layer.handle [as handle_request] (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\layer.js:95:5)
at next (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\layer.js:95:5)
at <Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\index.js:277:22
at param (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\index.js:349:14)
at param (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\index.js:410:3)
at next (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\index.js:271:10)
at Function.handle (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\index.js:176:3)
at router (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\index.js:46:12)
at Layer.handle [as handle_request] (<Workspace>\TestLoopback\loopback-test\node_modules\express\lib\router\layer.js:95:5)
Additional information
The error seems to occur because in line 164 of model.js it is doing if (id && data) { As data is null and id = 0 (which is still a correct state here) it will abort processing the request by throwing the error.
Using loopback-cli and latest loopback version.
I made a small test modifying the model.js file to do a check like
if(id!=null)
instead of
if(id)
which won't cause problems when trying to get child items of models with id = 0
After a little bit of looking around this only seems to be an issue for me as i'm using an oracle table that allows PK columns to be 0. Other databases like MySQL won't even allow that. I still feel though as this is a bug in the models.js file and could be easily fixed be making a more specific null-check or am i missing something?
I had the same issue (relations items from model id = 0). But I had to modify include.js file if (targetId) ==> if (targetId !== null) in the includeOneToOne method to make it work. No change in models.js. There were too heavy consequences of changing zero id in the model. My database is MySql. Don't know if i did well.
Hi @ExTheSea Thanks for reporting the issue. It is already been detected and on our list of issues To Be Fixed. Please follow up with these 2 issues: https://github.com/strongloop/loopback/issues/2374 https://github.com/strongloop/loopback/issues/2356
Thanks.