开发指南 Reference Source
public class | source

HasOne

Extends:

Association → HasOne

One-to-one association

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(). This is almost the same as belongsTo with one exception - The foreign key will be defined on the target model.

See:

Method Summary

Public Methods
public

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

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

public

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

Get the associated instance.

public

async set(sourceInstance: Model, associatedInstance: Model | string | number, options: object): Promise

Set the associated model.

Inherited Summary

From class Association
public

The type of the association.

public
public

Public Methods

public async create(sourceInstance: Model, values: object, options: object): Promise<Model> source

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

Params:

NameTypeAttributeDescription
sourceInstance Model

the source instance

values object
  • optional
  • default: {}

values to create associated model instance with

options object
  • optional

Options passed to target.create and setAssociation.

Return:

Promise<Model>

The created target model

See:

  • Model#create for a full explanation of options

public async get(instances: Model | Array<Model>, options: object): Promise<Model> source

Get the associated instance.

Params:

NameTypeAttributeDescription
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:

public async set(sourceInstance: Model, associatedInstance: Model | string | number, options: object): Promise source

Set the associated model.

Params:

NameTypeAttributeDescription
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

Options passed to getAssociation and target.save

Return:

Promise