Amplify Toolbox
    Preparing search index...
    • 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 the id field unless overwritten with the identifier() method.

      Type Parameters

      • RM extends string

      Parameters

      • relatedModel: RM

        the name of the related model

      • references: string | string[]

        the field(s) that should be used to reference the related model

      Returns ModelRelationshipField<
          ModelRelationshipTypeArgFactory<RM, hasOne, false>,
          RM,
          "valueRequired",
          undefined,
      >

      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')
      }),
      });