Documentation
    Preparing search index...

    Class HasOneAssociation<S, T, SourceKey, TargetKey, TargetPrimaryKey>

    One-to-one association. See Model.hasOne

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

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

    Type Parameters

    Hierarchy (View Summary)

    Index
    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>
    • 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(): TargetKey

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

      Returns TargetKey

    • get identifierField(): string

      The column name of the foreign key (on the target model)

      Returns string

    • 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 sourceKeyField(): string

      The Column Name of the source key.

      Returns string

    • get isMultiAssociation(): boolean

      Returns boolean

    • Set the associated model.

      Parameters

      • sourceInstance: S

        the source instance

      • associatedInstanceOrPk: T | T[TargetPrimaryKey]

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

      • Optionaloptions: HasOneSetAssociationMixinOptions<T>

        Options passed to getAssociation and target.save

      Returns Promise<T>

      The associated instance, or null if disassociated.

    • Set the associated model.

      Parameters

      • sourceInstance: S

        the source instance

      • associatedInstanceOrPk: null

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

      • Optionaloptions: HasOneSetAssociationMixinOptions<T>

        Options passed to getAssociation and target.save

      Returns Promise<null>

      The associated instance, or null if disassociated.