Parent

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

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

The parent relation will return the parent model that the supplied child references.

The opposite side of this relation is called Children.

final class Pet: Model {
    var userID: UUID
    ...
    var user: Parent<Pet, User> {
        return parent(\.userID)
    }
}

final class User: Model {
    var id: UUID?
    ...
}

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

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

  • Create a query for the parent.

    let pet: Pet
    pet.user.query(on: conn).first()
    

    Declaration

    Swift

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

    Parameters

    conn

    Database connection to use.

    Return Value

    Newly created QueryBuilder.

  • Convenience for getting the parent.

    let pet: Pet
    pet.user.get()
    

    This method will throw an error if no Parent is found.

    Declaration

    Swift

    public func get(on conn: DatabaseConnectable) -> Future<Parent>

    Parameters

    conn

    Database connection to use.

    Return Value

    The Parent.