Class BelongsTo<S, T, SourceKey, TargetKey>

One-to-one association See belongsTo

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

  • S extends Model = Model

    The model on which belongsTo has been called, on which the association methods, as well as the foreign key attribute, will be added.

  • T extends Model = Model

    The model passed to belongsTo.

  • SourceKey extends AttributeNames<S> = any

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

  • TargetKey extends AttributeNames<T> = any

    The name of the attribute that the foreign key in the source model will reference, typically the Primary Key.

Hierarchy

Constructors

Properties

#private: any
foreignKey: SourceKey
identifierField: string

The column name of the foreign key

inverse: undefined | Association<Model<any, any>, Model<any, any>, string, NormalizedAssociationOptions<string>>
isAliased: boolean
isSelfAssociation: boolean
options: NormalizedBelongsToOptions<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>
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 sourceKey). The foreignKey is on the Source Model.

targetKeyField: string

The column name of the target key

targetKeyIsPrimary: boolean

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 identifier(): string
  • The attribute name of the identifier

    Deprecated

    use foreignKey instead

    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
  • Creating an associations can automatically create other associations. This returns the initial association that caused the creation of the descendant associations.

    Returns Association<Model<any, any>, Model<any, any>, string, NormalizedAssociationOptions<string>>

  • get targetIdentifier(): string
  • get isMultiAssociation(): boolean
  • Returns boolean

Methods

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

    Returns

    The created target model

    Parameters

    Returns Promise<T>

  • inferForeignKey(): string
  • Returns string

  • Set the associated model.

    Parameters

    • sourceInstance: S

      the source instance

    • associatedInstance: null | T | T[TargetKey]

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

    • Optional options: BelongsToSetAssociationMixinOptions<T>

      options passed to this.save

    Returns Promise<void>

Generated using TypeDoc