I have an image model and a location model. The image model contains foreign key to location. To fetch the result I use:
fetch({withRelated: ['location']};
and i recieve the following results:
{
"id": 24,
"created_by": 1,
"location_id": 202,
"location": {}
}
But I want something like:
{
"id": 24,
"created_by": 1,
"location": {....}
}
My image model:
objectProperties = {
tableName: 'images',
location: function () {
return this.hasOne(location, 'id');
}
};
classProperties = {};
imageModel = bookshelf.Model.extend(objectProperties, classProperties);
and my location model:
objectProperties = {
tableName: 'locations',
images: function () {
return this.belongsToMany(image, 'location_id');
}
};
classProperties = {};
locationModel = bookshelf.Model.extend(objectProperties, classProperties);
Why do I receive an empty location object?
Your relations in models are wrong. You use belongsToMany (used for m:n relationships only) in combination with hasOne (cannot be used with belongsToMany). It is not clear from your question what kind of relationship do these two tables have so i cannot assist you further. But the problem is not in withRelated but in model definition. Hope this helps.