2

Is there any way to create a two foreign key relation in Objection.js. Something like following:

  static get relationMappings() {
    return {
      childs: {
        relation: Model.HasManyRelation,
        modelClass: childModel,
        join: {
          from: 'parent.id',
          to: 'child.parent_id'
        },
        join: { // Another foreign key
          from: 'parent.record_id',
          to: 'child.parent_record_id'
        }
      }
    }
  }
Rashomon
  • 5,962
  • 4
  • 29
  • 67

1 Answers1

5

Finally I found out the solution at docs

  static get relationMappings() {
    return {
      childs: {
        relation: Model.HasManyRelation,
        modelClass: childModel,
        join: {
          from: [
            'parent.id',
            'parent.record_id'
          ],
          to: [
            'child.parent_id',
            'child.parent_record_id'
          ]
        },
      }
    }
  }
Rashomon
  • 5,962
  • 4
  • 29
  • 67