Amplify Toolbox
    Preparing search index...
    • Create a one-directional one-to-many relationship between two models using the hasMany("MODEL_NAME", "REFERENCE_FIELD(s)") 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, hasMany, true>,
          RM,
          "required",
          undefined,
      >

      a one-to-many relationship definition

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