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

One-to-one association. See hasOne

This is almost the same as BelongsTo, 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

  • S extends Model = Model

    The model on which hasOne has been called, on which the association methods will be added.

  • T extends Model = Model

    The model passed to hasOne. This model will receive the Foreign Key attribute.

  • SourceKey extends AttributeNames<S> = any

    The name of the attribute that the foreign key in the target model will reference.

  • TargetKey extends AttributeNames<T> = any

    The name of the Foreign Key attribute on the Target model.

  • TargetPrimaryKey extends AttributeNames<T> = any

    The name of the Primary Key attribute of the Target model. Used by HasOneSetAssociationMixin.

Hierarchy

Constructors

Properties

#private: any
inverse: BelongsTo<T, S, TargetKey, SourceKey>
isAliased: boolean
isSelfAssociation: boolean
options: NormalizedHasOneOptions<SourceKey, TargetKey>
parentAssociation: null | Association<Model<any, any>, Model<any, any>, string, NormalizedAssociationOptions<string>>

A reference to the association that created this one.

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

Accessors

  • 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
  • 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
  • Deprecated

    negate isMultiAssociation instead

    Returns boolean

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

    • plural: string
    • singular: string
  • get sourceKey(): SourceKey
  • The name of the attribute the foreign key points to. In HasOne, it is on the Source Model, instead of the Target Model (unlike targetKey). The foreignKey is on the Target Model.

    Returns SourceKey

  • get sourceKeyAttribute(): SourceKey
  • Deprecated

    use sourceKey

    Returns SourceKey

  • get sourceKeyField(): string
  • The Column Name of the source key.

    Returns string

  • get isMultiAssociation(): boolean
  • Returns boolean

Methods

  • Create a new instance of the associated model and associate it with this.

    See Model#create for a full explanation of options.

    Returns

    The created target model

    Parameters

    Returns Promise<T>

  • Set the associated model.

    Returns

    The associated instance, or null if disassociated.

    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.

    • Optional options: HasOneSetAssociationMixinOptions<T>

      Options passed to getAssociation and target.save

    Returns Promise<T>

  • Parameters

    Returns Promise<null>

Generated using TypeDoc