hasOne Relationship Method - API Reference

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.
optionsRelationshipOptionsOptional
The relationship's options.
Was this page helpful?