Children

public struct Children<Parent, Child>
    where Parent: Model, Child: Model, Parent.Database == Child.Database

The children relation is one side of a one-to-many database relation.

The children relation will return all the models that contain a reference to the parent’s identifier.

The opposite side of this relation is called Parent.

final class Pet: Model {
    var userID: UUID
    ...
}

final class User: Model {
    var id: UUID?
    ...
    var pets: Children<User, Pet> {
        return children(\.userID)
    }
}

Create a new Children relation using the Model.children(...) method. The method accepts a key path to the child’s property that references its parent’s ID.

Children structs (and other relations) are usually generated by stored properties on the model.

  • Reference to the parent. This will be used later to access the ID.

    Declaration

    Swift

    public var parent: Parent
  • Create a new QueryBuilder for the children relation.

    let user: User
    user.pets.query(on: conn).all()
    

    Declaration

    Swift

    public func query(on conn: DatabaseConnectable) throws -> QueryBuilder<Child.Database, Child>

    Parameters

    conn

    Database connection to use.

    Return Value

    Newly created QueryBuilder.