Many-to-many relationships
A many-to-many relationship describes the case where multiple records in one object class are related to multiple objects in another object class.
A Person has many Skills, and each Skill has many Persons.
A Project has many Project members (Persons), and each Person is a member of many Projects.
Appfarm supports multi-references, i.e. Object Class Properties with Cardinality Many, to hold a list of references towards an Object Class or Enumerated Types. This is a great option if you only need information about references. For example: Person.Skills
may be a multi-reference towards the Skill
object class, if you only need to know the list of skills for a person (and which persons that have a certain skill) - and you do not need to know e.g. which level a person has.
Note that you may also define multi-reference properties as runtime properties on data sources, or App variables.
Multi-references (Object Class Properties with cardinality Many) are set up in the Global Data Model and may be used in the UI (Tables, Multi Select, List ++) and Logic (Foreach, Conditions ++) directly. This guide highlights the key features, but you may find these articles useful as well:
This guide describes how to create many-to-many relationships in Appfarm Create.
Example of a many-to-many relationship (using multi-reference)
Say you have an object class Person
to store all your organization's employees and an object class Skill
to store all the skills relevant to your organization. You would like to be able to track which employees have which skills (but not which level).
Since a Person
can have multiple Skills
, and the Skills
are considered to be information related to the Person
(and not Persons being information related to Skills), we need the following:
A
Person
object classA
Skill
object classA property
Person.Skills
, with a reference towards theSkill
object class, with CardinalityMany
.
Multi-reference: Key features
Person.Skills
may contain a list of Skill ID
s, and in general you may treat this property as a Data Source with Cardinality Many. Here are some key features:
Person.Skills
can be set in a Create Object or an Update Object action node, or directly in the UI using a Multi Select (with selection type Property)You may add or remove IDs to/from
Person.Skills
using Update Object and a value processor, as in this example.You may iterate
Person.Skills
in a Foreach Action Node or an Iterating Container or Table in your UI.
However, if you need to store information about what level each Person has for each Skill, you may not use the above approach. See the following example.
Example of a many-to-many relationship (using a many-to-many Object Class)
Given the above example: We now want to save information about what Skill Level each Person has for each Skill.
In that case, we need the following:
A
Person
object classA
Skill
object classAn enumerated type
Skill level
A
Person Skills
object class
Person Skills
is a many-to-many relationship object class that contains references to the two object classes to be related: Person and Skills. In addition, it has a reference to the enum Skill level. For each Person's skill, a record should be created into this Object Class (holding the ID of the Person and Skill, and the Value of the Skull level enum).
To create the Person Skills
object class:
Step 1
Add a new object class to your data model. The best practice is to name this object class using a combination of the names of the two object classes to be related, for example Person Skill
.
Step 2
Add a new property to the newly created object class. This property will store references to another object class that represents one half of the relationship.
Property Name: Best practice is to use the name of the object class, for example
Person
.Data Type: Select the object class to relate under References, in this case
Person
.Cardinality: One.
Step 3
Add another new property to the object class. This property will store references to another object class that represents the other half of the relationship.
Property Name: Best practice is to use the name of the object class, for example
Skill
.Data Type: Select the object class to relate under References, in this case
Skill
.Cardinality: One.
Step 4
Add another new property to the enumerated type Skill level.
Property Name: Best practice is to use the name of the object class, for example,
Skill level
or justLevel
Data Type: Select the enumerated type to relate under Enums, in this case
Skill level
.Cardinality: One.
Now you have an object class that references two other object classes. Each record added to the object class represents one connection between the two object classes. For example, an individual person and one skill that they possess, and the level. Each employee will have a unique record for each skill they possess and each skill will be represented based on how many employees possess that skill.
Note that to use these object classes in an app or service you need to add data sources.
Last updated