繁体   English   中英

如何在 BookShelf.js 中获取 hasMany 关系

[英]How to fetch hasMany relation in BookShelf.js

我正在尝试从报价 model 中获取数据,该数据与投资 Model 具有“hasMany”关系,但我总是收到错误“必须为报价定义的有效目标 Z20F35E630DAF44DBFA4C3F68F5399D8 关系C3F68F5399D8”

报价 Model:

const Investment = require('./investment')

const Offer = bookShelfDatabase.Model.extend({
    tableName: 'offers',
    investment: function() {
        return this.hasMany(Investment);
      }
});

module.exports = Offer;

投资Model:

const Offer = require('./offer');


const Investment = bookShelfDatabase.Model.extend({
    tableName: 'investments',
    offers: function () {
        return this.belongsTo(Offer, 'offer_id');
      }
});

module.exports = Investment;

我的服务

const offers = await new Offer({ id: 1 }).fetchAll({
      withRelated: [
        {
          'investment': function (qb) {
            qb.select("id");
          },
        },
      ],
    });

你有一个循环依赖问题。 Offer需要InvestmentInvestment需要Offer ,这不起作用。

使用推荐的方法( .model() )而不是.extend()创建模型,你会没事的: https://bookshelfjs.org/tutorial-models.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM