Amplify Toolbox
    Preparing search index...
    • Use belongsTo() to create a field to query the related hasOne() or hasMany() relationship. The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from parent to the related model.

      Type Parameters

      • RM extends string

      Parameters

      • relatedModel: RM

        name of the related .hasOne() or .hasMany() model

      • references: string | string[]

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

      Returns ModelRelationshipField<
          ModelRelationshipTypeArgFactory<RM, belongsTo, false>,
          RM,
          "required" | "valueRequired",
          undefined,
      >

      a belong-to relationship definition

      // one-to-many relationship
      const schema = a.schema({
      Member: a.model({
      name: a.string().required(),
      // 1. Create a reference field
      teamId: a.id(),
      // 2. Create a belongsTo relationship with the reference field
      team: a.belongsTo('Team', 'teamId'),
      })
      .authorization(allow => [allow.publicApiKey()]),

      Team: a.model({
      mantra: a.string().required(),
      // 3. Create a hasMany relationship with the reference field
      // from the `Member`s model.
      members: a.hasMany('Member', 'teamId'),
      })
      .authorization(allow => [allow.publicApiKey()]),
      });
      // one-to-one relationship
      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')
      }),
      });