0

Bookshelfとの関係を作成しようとしていますMerchantsTransactions

  • 1 つのトランザクションで複数のバウチャーを使用できます
  • 1 つのバウチャーには、トランザクション キーとマーチャント キーが含まれています
  • マーチャントは、すべての固有のトランザクションを引き出すことができる必要があります

私のモデルは以下のように定義されています:

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}
  }
};
4

1 に答える 1

0

問題はスキーマにあると思います。マーチャントもトランザクションも、いずれかの ID を所有していません。現在、それらに参加することはできません。おそらくスキーマを少し変更しますか?

于 2015-04-19T07:43:30.657 に答える