Back to Sequelize

BelongsTo

static/v5/class/lib/associations/belongs-to.js~BelongsTo.html

latest7.8 KB
Original Source

publicclass| source

BelongsTo

Extends:

Association → BelongsTo

One-to-one association

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().

See:

Method Summary

| Public Methods | | public |

create(sourceInstance: Model, values: Object, options: Object): Promise<Model>

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

| | | public |

get(instances: Model | Array<Model>, options: Object): Promise<Model>

Get the associated instance.

| | | public |

set(sourceInstance: Model, associatedInstance: <Model> | string | number, options: Object): Promise

Set the associated model.

| |

Inherited Summary

| From class Association | | public |

associationType: string

The type of the association.

| | | public |

source: Model

| | | public |

target: Model

| |

Public Methods

publiccreate(sourceInstance: Model, values: Object, options: Object): Promise<Model> source

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

Params:

| Name | Type | Attribute | Description | | sourceInstance | Model | |

the source instance

| | values | Object |

  • optional
  • default: {}

|

values to create associated model instance with

| | options | Object |

  • optional
  • default: {}

|

Options passed to target.create and setAssociation.

|

Return:

| Promise<Model> |

The created target model

|

See:

  • Model#create for a full explanation of options

publicget(instances: Model | Array<Model>, options: Object): Promise<Model> source

Get the associated instance.

Params:

| Name | Type | Attribute | Description | | instances | Model | Array<Model> | |

source instances

| | options | Object |

  • optional

|

find options

| | options.scope | string | boolean |

  • optional

|

Apply a scope on the related model, or remove its default scope by passing false.

| | options.schema | string |

  • optional

|

Apply a schema on the related model

|

Return:

| Promise<Model> |

See:

publicset(sourceInstance: Model, associatedInstance: <Model> | string | number, options: Object): Promise source

Set the associated model.

Params:

| Name | Type | Attribute | Description | | sourceInstance | Model | |

the source instance

| | associatedInstance | <Model> | string | number |

  • optional
  • nullable: true

|

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

| | options | Object |

  • optional
  • default: {}

|

options passed to this.save

| | options.save | boolean |

  • optional
  • default: true

|

Skip saving this after setting the foreign key if false.

|

Return:

| Promise |