Compartir a través de


Adding References Between Extents

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

When creating a model in “M”, you will typically need to define relationships between the extents. This topic shows how to create these relationships.

Referencing Extents

Before one extent can reference another extent, the referenced extent must have an identity. For more information, see Choosing an Identity Field for each Extent. To reference another extent, create a field in your current extent and specify the type of that field to be the referenced extent name. The following example illustrates this pattern.

module Contact
{
    PhoneNumbers :
    {( 
        {
            Id : Integer64 => AutoNumber;
            
            Work : Text;

            Home : Text?;

            Mobile : Text?;
        }
    )*} where identity Id;
    
    Hometowns :
    {(
        {
            Id : Integer64 => AutoNumber;
            
            City : Text;
            
            State : Text;                       
        }
    )*} where identity Id;
    
    ContactDetails :
    {(
        {
            Id : Integer64 => AutoNumber;
            
            LastName : Text;
            
            FirstName : Text;
            
            PhoneInfo : PhoneNumbers;
            
            Hometown : Hometowns;
        } 
    )*} where identity Id;
}

In this example, there are two extents that describe parts of a contact's information: PhoneNumbers and Hometowns. There is a third extent, ContactDetails, which represents the complete contact information for one individual. The PhoneInfo field specifies a type of PhoneNumbers, which is the first extent. The Hometown field specifies a type of Hometowns, which is the second extent.

For more information about references in “M”, see Relationships.

Adding Initial Values to Extents with References

You can use “M” to add initial values to an extent. The following example shows how to add a single record to the PhoneNumbers extent.

module Contact
{
    PhoneNumbers
    {
        { Work => "555-555-5555" }
    }
}

Although the previous example successfully inserts a new record with the specified phone number information, there is no way to specify this new record when creating an extent that references the PhoneNumbers extent. To reference this new record, add a label to the new record.

    PhoneNumbers
    {
        Phone1 { Work => "555-555-5555" }
    }

In this modified code example, the label Phone1 precedes the first brace of the record. With this change another extent can refer to this record using the extent and label names, as in PhoneNumbers.Phone1. The following example code shows how labels can be used in this way.

module Contact
{
    PhoneNumbers
    {
        Phone1 { Work => "555-555-5555" }
    }
    
    Hometowns 
    {
        StauntonVA { City => "Staunton", State => "Virginia" }
    }
    
    ContactDetails
    {
        NancyAnderson 
        { 
            LastName => "Anderson", 
            FirstName => "Nancy",
            PhoneInfo => PhoneNumbers.Phone1, 
            Hometown => Hometowns.StauntonVA 
        }
    }
}

In this example, the ContactDetails extent contains a PhoneInfo field that references the PhoneNumbers extent. It also contains a Hometown field that references the Hometowns extent. To add a new record to the ContactDetails extent, it is first necessary to add a labeled record to the PhoneNumbers and Hometowns extents. This is done in the example above with the Phone1 and StauntonVA records. The new ContactDetails record can then assign the PhoneInfo field to PhoneNumbers.Phone1 and the Hometown field to Hometowns.StauntonVA.

See Also

Concepts

Basic Data Modeling Patterns

Other Resources

Relationships