the name of the related model
the field(s) that should be used to reference the related model
a one-to-one relationship definition
const schema = a.schema({
Cart: a.model({
items: a.string().required().array(),
// 1. Create reference field
customerId: a.id(),
// 2. Create relationship field with the reference field
customer: a.belongsTo('Customer', 'customerId'),
}),
Customer: a.model({
name: a.string(),
// 3. Create relationship field with the reference field
// from the Cart model
activeCart: a.hasOne('Cart', 'customerId')
}),
});
Create one-to-one relationship between two models using the
hasOne("MODEL_NAME", "REFERENCE_FIELD(s)")
method. A hasOne relationship always uses a reference to the related model's identifier. Typically this is theid
field unless overwritten with theidentifier()
method.