hasOne Relationship Method - DML Reference
hasOne<TypeParameter T, TypeParameter ForeignKeyName>(entityBuilder, options): HasOneWithForeignKey<T, ForeignKeyName>#
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#
1import { model } from "@medusajs/framework/utils"2
3const User = model.define("user", {4 id: model.id(),5 email: model.hasOne(() => Email),6})
Type Parameters#
The type of the entity builder passed as a first parameter. By default, it's
a function returning the related model.
ForeignKeyName
undefined | stringOptional
Parameters#
A function that returns the data model this model is related to.
options
object & Record<string, any> & object
The relationship's options.
hasOne<TypeParameter T>(entityBuilder, options?): HasOne<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#
1import { model } from "@medusajs/framework/utils"2
3const User = model.define("user", {4 id: model.id(),5 email: model.hasOne(() => Email),6})
Type Parameters#
The type of the entity builder passed as a first parameter. By default, it's
a function returning the related model.
Parameters#
A function that returns the data model this model is related to.
options
object & Record<string, any> & objectOptional
The relationship's options.
Custom Namespace#
Relationship Methods
Was this page helpful?