Relations broken when using format/parse
See original GitHub issueOur build started breaking randomly today and I finally figured out that it was after the latest update of Bookshelf that you broke something related to ids.
All our tables use underscore as separator in the column names and camelCase in the models (As outlined in the example for model.parse).
One of our tables also uses one of these values as its ID attribute which has now broken relations for that table, fetches using withRelated
for that table no longer return any results.
I have created the following gist: https://gist.github.com/richardsimko/fee44b444df3b3e768cdd530311dbd3d to illustrate the problem. Running broken.js
will print the relation as length 0 and running working.js
will print length 1. The only difference is the name of the ID attribute (clientKey
vs client_key
).
If I roll back to an earlier version than 0.10.3 both files work again.
EDIT: Thanks to installing from Git I have found that the issue started after #1397 was merged.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:6
- Comments:9 (4 by maintainers)
I recommend Objection.js, it’s also based on knex and well documented and maintained.
The culprit is here: https://github.com/tgriesser/bookshelf/blob/1860604f57accdce19c6a1af0ef65bbdaf5b8531/src/relation.js#L398
If
this.parentIdAttribute
doesn’t match the name of the attribute in the target model it doesn’t work. Of course in this case they won’t match because obviously one is in snake_case and the other in camelCase.At this point
model
is not an instance of my model but instead just an instance ofBookshelfModel
otherwise calling.parse
on it could have worked.Any workarounds are welcome as well as suggested fixes, I think I’ve done as much as I’m able to with my limited knowledge of the code base.
Actually on second though the behavior on that line has changed completely and I’m wondering if that may be an accident? It’s changed from
model.id
(I.e. the ID of the target model) tomodel.get(this.parentIdAttribute)
(I.e. the ID of the parent model fetched from the target model). I may be wrong here but those aren’t supposed to be the same?