# Composite keys

Composite (compound) keys are fully supported. Just give an array of columns where you would normally give a single column name. Composite primary key can be specified by setting an array of column names to the idColumn of a model class.

Here's a list of methods that may help working with composite keys:

# Examples

Specifying a composite primary key for a model:

class Person extends Model {
  static get idColumn() {
    return ['firstName', 'lastName', 'dateOfBirth'];
  }
}

Specifying a relation using a composite primary key and a composite foreign key:

class Person extends Model {
  static get tableName() {
    return 'persons';
  }

  static get relationMappings() {
    return {
      pets: {
        relation: Model.BelongsToOneRelation,
        modelClass: Animal,
        join: {
          from: [
            'persons.firstName',
            'persons.lastName',
            'persons.dateOfBirth'
          ],
          to: [
            'animals.ownerFirstName',
            'animals.ownerLastName',
            'animals.ownerDateOfBirth'
          ]
        }
      }
    };
  }
}

findById:

await Person.query().findById([1, 'something', 7]);

whereComposite:

await Person.query().whereComposite(['foo', 'bar'], [1, 'barValue']);

whereInComposite:

await Person.query().whereInComposite(
  ['foo', 'bar'],
  [
    [1, 'barValue1'],
    [2, 'barValue2']
  ]
);