hasOne Relationship Method - DML Reference

hasOne<TypeParameter T>(entityBuilder, options): HasOneWithForeignKey<T>#

This method defines a relationship between two data models, where the owner of the relationship has one record of the related data model.

For example: A user "hasOne" email.

Use the belongsTo method to define the inverse of this relationship in the other data model.

Example#

Code
1import { model } from "@medusajs/framework/utils"2
3const User = model.define("user", {4  id: model.id(),5  email: model.hasOne(() => Email),6})

Type Parameters#

TobjectOptional
The type of the entity builder passed as a first parameter. By default, it's a function returning the related model.

Parameters#

entityBuilderT
A function that returns the data model this model is related to.
optionsobject & Record<string, any> & object
The relationship's options.

hasOne<TypeParameter T>(entityBuilder, options?): HasOne<T>#

Type Parameters#

TobjectOptional

Parameters#

entityBuilderT
optionsobject & Record<string, any> & objectOptional
Was this page helpful?