# 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:
- whereComposite
- whereInComposite
- findById
- findByIds
- deleteById
- updateAndFetchById
- patchAndFetchById
- $id
# 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'
]
}
}
};
}
}
await Person.query().findById([1, 'something', 7]);
await Person.query().whereComposite(['foo', 'bar'], [1, 'barValue']);
await Person.query().whereInComposite(
['foo', 'bar'],
[
[1, 'barValue1'],
[2, 'barValue2']
]
);