如何通过连接Bookshelf.js创建模型关系

时间:2015-03-17 12:05:10

标签: node.js bookshelf.js

我正在尝试使用Bookshelf在MerchantsTransactions之间创建关系。

  • 一个交易可以有多个优惠券
  • 一张优惠券包含交易密钥和商家密钥
  • 商家需要能够提取所有独特的交易

我的模型定义如下:

var Voucher = Bookshelf.Model.extend({

  tableName: 'vouchers',

  transaction: function() {
    return this.belongsTo(Transaction);
  },

  merchant: function() {
    return this.belongsTo(Merchant);
  }
});


var Transaction = Bookshelf.Model.extend({

  tableName: 'transactions',

  vouchers: function() {
    return this.hasMany(Voucher);
  }
});


var Merchant = Bookshelf.Model.extend({

  tableName: 'merchants',


  vouchers: function() {
    return this.hasMany(Voucher);
  },

  transactions: function() {
    //???
  }
});

模式

var schema = {
  merchants: {
    id: {type: 'increments', nullable: false, primary: true},
    category_id: {type: 'integer', nullable: false, unsigned: true, references: 'categories.id'},
    name: {type: 'string', maxlength: 150, nullable: false},
    slug: {type: 'string', maxlength: 150, nullable: false, unique: true},
    email: {type: 'string', maxlength: 254, nullable: false},
    tel: {type: 'string', maxlength: 64, nullable: false},
    location_name: {type: 'string', maxlength: 150, nullable: false},
    province: {type: 'string', maxlength: 64, nullable: false},
    city: {type: 'string', maxlength: 254, nullable: false},
    town: {type: 'string', maxlength: 254, nullable: true},
    address: {type: 'text', maxlength: 2000, nullable: false},
    created_at: {type: 'dateTime', nullable: false},
    updated_at: {type: 'dateTime', nullable: true}
  },

  transactions: {
    id: {type: 'increments', nullable: false, primary: true}, 
    user_id: {type: 'integer', nullable: false, unsigned: true, references: 'users.id'},
    uuid: {type: 'string', maxlength: 128, nullable: false},
    raw: {type: 'text', maxlength: 2000, nullable: false},
    total_amount: {type: 'integer', nullable: false},
    total_vouchers: {type: 'integer', nullable: false},
    total_postages: {type: 'integer', nullable: true},
    created_at: {type: 'dateTime', nullable: false},
    updated_at: {type: 'dateTime', nullable: true}
  },

  vouchers: {
    id: {type: 'increments', nullable: false, primary: true}, 
    transaction_id: {type: 'integer', nullable: false, unsigned: true, references: 'transactions.id'},
    merchant_id: {type: 'integer', nullable: false, unsigned: true, references: 'merchants.id'},
    user_id: {type: 'integer', nullable: false, unsigned: true, references: 'users.id'},
    vouchertemplate_id: {type: 'integer', nullable: false, unsigned: true, references: 'vouchertemplates.id'},
    voucher_number: {type: 'string', maxlength: 254, nullable: false},
    value: {type: 'integer', nullable: false},
    redeemed_value: {type: 'integer', nullable: false, defaultTo: 0},
    message: {type: 'string', maxlength: 254, nullable: false},
    recipient_name: {type: 'string', maxlength: 254, nullable: false},
    created_at: {type: 'dateTime', nullable: false},
    updated_at: {type: 'dateTime', nullable: true}
  }
};

1 个答案:

答案 0 :(得分:0)

我认为问题出在您的架构中。无论是商家还是交易都没有任何身份证明。目前您无法加入他们。也许稍微修改架构?