function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
developer-forcedeveloper-force 

Using relation-ship in SOQL

I have two custom objects

City and Location. I want to create a relationship between City and Location in a manner where one city can have many locations. I have modeled this using Master-detail relationship.

On the VisualForce page I have a select list with cities and on selecting a city from the select list using actionSupport I retrieve location list corresponding to the city selected.

My Custom city object structure is as below
Code:
<—xml version="1.0" encoding="UTF-8"–>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <deploymentStatus>Deployed</deploymentStatus>
    <description>Object for city</description>
    <enableActivities>true</enableActivities>
    <fields>
        <fullName>city__c</fullName>
        <indexed>true</indexed>
        <label>City</label>
        <length>100</length>
        <required>true</required>
        <type>Text</type>
        <unique>true</unique>
    </fields>
    <fields>
        <fullName>id__c</fullName>
        <displayFormat>CT-{00000000}</displayFormat>
        <label>ID</label>
        <type>AutoNumber</type>
    </fields>
    <label>City</label>
    <nameField>
        <displayFormat>CT-{00000000}</displayFormat>
        <label>city_id</label>
        <type>AutoNumber</type>
    </nameField>
    <pluralLabel>Cities</pluralLabel>
    <sharingModel>ReadWrite</sharingModel>
</CustomObject>

 
Location custom object

Code:
<—xml version="1.0" encoding="UTF-8"–>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <deploymentStatus>Deployed</deploymentStatus>
    <description>Object for location</description>
    <enableActivities>true</enableActivities>
    <fields>
        <fullName>city_id__c</fullName>
        <label>City</label>
        <referenceTo>City__c</referenceTo>
        <relationshipLabel>Locations</relationshipLabel>
        <relationshipName>Locations</relationshipName>
        <relationshipOrder>0</relationshipOrder>
        <type>MasterDetail</type>
    </fields>
    <fields>
        <fullName>id__c</fullName>
        <displayFormat>LC-{00000000}</displayFormat>
        <label>ID</label>
        <type>AutoNumber</type>
    </fields>
    <fields>
        <fullName>location__c</fullName>
        <indexed>true</indexed>
        <label>Location</label>
        <length>100</length>
        <required>true</required>
        <type>Text</type>
        <unique>true</unique>
    </fields>
    <label>Location</label>
    <nameField>
        <label>Location Name</label>
        <type>Text</type>
    </nameField>
    <pluralLabel>Locations</pluralLabel>
    <sharingModel>ControlledByParent</sharingModel>
</CustomObject>

When I create a Location record instead of the City__c.id__c value a random value is stored in the Location__c.city_id__c which is relationship field.

I feel I did not understand the SObjects storage and retrieval as required. I would appreciate  any help towards my understanding of the same.


developer-forcedeveloper-force
I was able to figure out the relationship and SOQL associated with it. Below is the code snippet.
Code:
for(City__c city2 : [select (select id__c, location_name__c 
from Locations__r) from City__c where id__c = :city.id__c]) { locations = city2.Locations__r; }