Relation<R, F extends Fields> constructor

Relation<R, F extends Fields>(
  1. Table<R, F> target, {
  2. required List<Expr<Object?>> parent,
  3. required List<ReadField<Object?>> child(
    1. F
    ),
})

Describes an equality relationship between parent and target key columns.

parent and child must have the same nonzero arity. Child fields must belong to the supplied target occurrence. A null parent key has no match. Generated relationship getters normally construct this value for you.

Implementation

factory Relation(
  Table<R, F> target, {
  required List<Expr<Object?>> parent,
  required List<ReadField<Object?>> Function(F) child,
}) {
  final alias = target.alias();
  final fields = alias.fields;
  final keys = child(fields);
  if (parent.isEmpty ||
      parent.length != keys.length ||
      keys.any((key) => key.table != fields.table)) {
    throw ArgumentError(
      'Relationship keys need equal non-zero arity and fields from the target occurrence.',
    );
  }
  return Relation.internal(
    alias,
    List.unmodifiable(parent),
    List.unmodifiable(keys),
    fields,
    QueryState(fields.table),
    target.selectRow(fields),
  );
}