First thing you need to do when modeling a new object is to set up the object's key. The following guide describes how to set up a simple single field key for a primary object. For more complex scenarios you may want to read on
how to work with composite keys and
how to define child objects.
Here are the steps involved in defining a simple object key.
- First, you generally need to define a dedicated logical type in the model for the key field of your object, which you will not be able to use for any other object's key field. The only exception to this rule is when your key field needs to just reference the key of another already existing object, which typically happens either in child objects or when there is a 1-1 or 0-1 relationship between two objects.
- Add a field element to the fields group element of your object.
- Set the name attribute to the name of your key field.
- Set the type attribute to the dedicated type for your object key. If the key field just references another object's key then set it to the dedicated type for the referenced object's key.
- If the key value is auto-generated then set the key attribute to serial.
- If the key value is entered/supplied by the user then set the key attribute to supplied.
- If the key is a reference to another object's key then set the key attribute to reference.
- Set the required attribute to true if the key cannot be null.
- Provide a description of the key field in its child element doc > summary as necessary.
Below is an example that demonstrates these steps.
Code:
<module xmlns="http://www.xomega.net/omodel">
<types>
<type name="order number" base="integer"/>
</types>
<objects>
<object name="order header">
<fields>
<field name="order" type="order number" key="serial" required="true">
<doc>
<summary>The auto-generated key of the order header.</summary>
</doc>
</field>
<field name="customer" type="customer"/>
</fields>
</object>
<object name="order details">
<fields>
<field name="order" type="order number" key="reference" required="true">
<doc>
<summary>A reference to the order's key.</summary>
</doc>
</field>
<field name="product" type="product"/>
</fields>
</object>
</objects>
</module>