Skip to content

BelongsToRelationship

Josh Wright edited this page Jan 14, 2021 · 4 revisions

BelongsToRelationship

The child of a 1 - M or a 1 - 1 relationship. Backed by an identifier of the parent, when encoded to a database, this type attempt to write that identifier to a column named <property-name>_id.

@propertyWrapper public final class BelongsToRelationship<Child: Model, Parent: ModelMaybeOptional>: AnyBelongsTo, Codable, Relationship

Example:

struct Pet: Model {
    static let table = "pets"
    ...

    @BelongsTo
    var owner: User // The ID value of this User will be stored
                    // under the `owner_id` column in the
                    // `pets` table.
}

Inheritance

AnyBelongsTo, Codable, ExpressibleByNilLiteral, Relationship

Nested Type Aliases

From

public typealias From = Child

To

public typealias To = Parent

Initializers

init(_:)

Initialize this relationship with an Identifier of the Parent type.

public init(_ parentID: Parent.Value.Identifier)

Parameters

  • parentID: the identifier of the Parent to which this child belongs.

init(_:)

Initialize this relationship with an instance of Parent.

public init(_ parent: Parent.Value)

Parameters

  • parent: The Parent object to which this child belongs.

init(from:)

public init(from decoder: Decoder) throws

Properties

id

The identifier of this relationship's parent.

var id: Parent.Value.Identifier!

wrappedValue

The related Model object. Accessing this will fatalError if the relationship is not already loaded via eager loading or set manually.

var wrappedValue: Parent

projectedValue

The projected value of this property wrapper is itself. Used for when a reference to the relationship type is needed, such as during eager loads.

var projectedValue: Child.BelongsTo<Parent>

Methods

loadRelationships(for:query:into:)

public func loadRelationships(for from: [Child], query nestedQuery: @escaping (ModelQuery<Parent.Value>) -> ModelQuery<Parent.Value>, into eagerLoadKeyPath: KeyPath<Child, Child.BelongsTo<Parent>>) -> EventLoopFuture<[Child]>

encode(to:)

public func encode(to encoder: Encoder) throws
Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally