Documentation
    Preparing search index...

    Class BelongsToAssociation<S, T, SourceKey, TargetKey>

    One-to-one association See Model.belongsTo

    This is almost the same as HasOneAssociation, but the foreign key will be defined on the source model.

    In the API reference below, add the name of the association to the method, e.g. for User.belongsTo(Project) the getter will be user.getProject().

    Type Parameters

    Hierarchy (View Summary)

    Index
    identifierField: string

    The column name of the foreign key

    inverse:
        | Association<
            Model<any, any>,
            Model<any, any>,
            string,
            NormalizedAssociationOptions<string>,
        >
        | undefined
    isAliased: boolean
    isSelfAssociation: boolean
    parentAssociation:
        | Association<
            Model<any, any>,
            Model<any, any>,
            string,
            NormalizedAssociationOptions<string>,
        >
        | null

    A reference to the association that created this one.

    source: ModelStatic<S>
    target: ModelStatic<T>
    targetKey: TargetKey

    The name of the attribute the foreign key points to. In belongsTo, this key is on the Target Model, instead of the Source Model (unlike HasOneAssociation.sourceKey). The Association.foreignKey is on the Source Model.

    targetKeyField: string

    The column name of the target key

    targetKeyIsPrimary: boolean
    • get as(): string

      The identifier of the relation on the source model.

      Returns string

    • get associationType(): string

      The type of the association. One of HasMany, BelongsTo, HasOne, BelongsToMany

      Returns string

    • get foreignKey(): SourceKey

      The name of the foreign key attribute used by this association.

      Returns SourceKey

    • get identifier(): string

      The attribute name of the identifier

      Returns string

      use foreignKey instead

    • get isMultiAssociation(): boolean

      Returns boolean

    • get isSingleAssociation(): boolean

      Returns boolean

      negate isMultiAssociation instead

    • get name(): { plural: string; singular: string }

      Returns { plural: string; singular: string }

    • get rootAssociation(): Association

      Creating an associations can automatically create other associations. This returns the initial association that caused the creation of the descendant associations.

      Returns Association

    • get isMultiAssociation(): boolean

      Returns boolean

    • Returns string

    • Set the associated model.

      Parameters

      • sourceInstance: S

        the source instance

      • associatedInstance: T | T[TargetKey] | null

        An persisted instance or the primary key of an instance to associate with this. Pass null to remove the association.

      • Optionaloptions: BelongsToSetAssociationMixinOptions<T>

        options passed to this.save

      Returns Promise<void>