loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Model relation call fails when id is 0

Open ExTheSea opened this issue 9 years ago • 4 comments

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.

ExTheSea avatar Jan 23 '17 10:01 ExTheSea

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

ExTheSea avatar Jan 23 '17 10:01 ExTheSea

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?

ExTheSea avatar Jan 23 '17 16:01 ExTheSea

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.

pierreandreline avatar Jan 24 '17 13:01 pierreandreline

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.

loay avatar Jan 24 '17 19:01 loay