Adding a reference to an object with a composite key in Xomega is implicit and very straightforward and is based on the Xomega's way of
defining objects with composite keys. You can also read on
how to reference an object with a single key separately. Note, that there is also an implicit relationship between a child object and its parent object, which you can also learn about by reading on
how to define child objects.
The key of the object you want to reference should use a dedicated fieldset defined specifically for that object's key, which cannot be used for a key of another object. To add a reference to this object from another object you need to perform the following steps.
- Add a new fieldset element to the fields group element of the object that you add the reference to.
- Set the ref attribute of the fieldset to that dedicated fieldset of the referenced object's key. This will automatically establish a relationship between these two objects.
- Set the name attribute if your object may have another reference to this fieldset or have fields with the same names as the fields in the fieldset.
- If you want a better control over the database foreign key generated from this relationship then you can add a config > sql:composite-foreign-key child element to the field element, where the sql prefix should map to the namespace http://www.xomega.net/sql.
- If you want to override the name of the foreign key generated for the database, then you can set the name attribute accordingly.
- If you want to override the update or delete actions, then you can set the corresponding attributes as appropriate.
- You can also specify to NOT generate the foreign key by setting the mode attribute to none.
- Specify the description of the fieldset by adding the doc > summary inside the fieldset element.
Below is an example that illustrates this approach.
Code:
<module xmlns="http://www.xomega.net/omodel"
xmlns:sql="http://www.xomega.net/sql">
<types>
<type name="invoice line item" base="integer"/>
<type name="task" base="integer"/>
</types>
<fieldsets>
<fieldset name="timesheet key">
<field name="person" type="employee"/>
<field name="date" type="date"/>
<field name="task" type="task"/>
</fieldset>
</fieldsets>
<objects>
<object name="timesheet">
<fields>
<fieldset ref="timesheet key" key="supplied" required="true"/>
<field name="hours" type="decimal" required="true"/>
<field name="work description" type="memo"/>
</fields>
</object>
<object name="invoice line item">
<fields>
<field name="item" type="invoice line item" key="serial" required="true"/>
<fieldset ref="timesheet key" required="false">
<config>
<sql:composite-foreign-key name="FK_LineItem_TimeSheet" delete="no action"/>
</config>
<doc>
<summary>A reference to the timesheet object with a custom foreign key configuration.</summary>
</doc>
</fieldset>
<field name="amount" type="money"/>
</fields>
</object>
</objects>
</module>