Bookshelf js fetch withRelated选项返回空的关系对象

我有一个图像模型和一个位置模型。 图像模型包含外键到位置。 要获取我使用的结果:

fetch({withRelated: ['location']}; 

我收到以下结果:

 { "id": 24, "created_by": 1, "location_id": 202, "location": {} } 

但是我想要的东西是:

 { "id": 24, "created_by": 1, "location": {....} } 

我的图像模型:

 objectProperties = { tableName: 'images', location: function () { return this.hasOne(location, 'id'); } }; classProperties = {}; imageModel = bookshelf.Model.extend(objectProperties, classProperties); 

和我的位置模型:

 objectProperties = { tableName: 'locations', images: function () { return this.belongsToMany(image, 'location_id'); } }; classProperties = {}; locationModel = bookshelf.Model.extend(objectProperties, classProperties); 

为什么我收到一个空的位置对象?

你在模型中的关系是错误的。 与hasOne结合使用belongsToMany(仅用于m:n关系)(不能与belongsToMany一起使用)。 从你的问题来看,这两张表有什么样的关系是不清楚的,所以我不能进一步帮助你。 但问题不在于相关而是在模型定义中。 希望这可以帮助。