1-to-1 Relationship Between Two Modules – Only One Side Updates

I created a 1-to-1 relationship between two modules using code (not Studio).

Here’s what I did:

  • I added a relate field in Module A for Module B.
  • I added a relate field in Module B for Module A.
  • When I create a record in Module A and link it with a record in Module B, the relationship shows correctly in Module A.
  • But when I do the reverse (create a record in Module B and link it with a record in Module A), the related field in Module B does not get filled.

My question is:

  • Do I need to define something differently in vardefs so both sides update automatically?
  • Or should this work without using logic hooks?

If I understand correctly, you added a relate field in A and a relate field in module B.

That won’t work one won’t populate the other. A relate field is a single relationship one way.

If you want one to one, you need to add a relataionhip, not a field.

I have added a relationship too using this code:

$dictionary['moduleb_modulet'] = array(
    'relationships' => array(
        'moduleb_modulet' => array(
            'lhs_module' => 'ModuleB',
            'lhs_table' => 'moduleb',
            'lhs_key' => 'id',
            'rhs_module' => 'ModuleT',
            'rhs_table' => 'modulet',
            'rhs_key' => 'moduleb_id',
            'relationship_type' => 'one-to-one',
        ),
    ),
);

Actually, I want to create a relationship using code (not studio).

I would try creating a one-to-one relationship with Studio, check if it works both ways, and then carefully checking the differences to your implementation.

  1. Do not use just relate fields—they don’t sync automatically.
  2. Define a proper 1-to-1 relationship in vardefs:
$dictionary["ModuleA"]["relationships"]["modulea_moduleb"] = [
    'lhs_module'=>'ModuleA','lhs_table'=>'modulea','lhs_key'=>'id',
    'rhs_module'=>'ModuleB','rhs_table'=>'moduleb','rhs_key'=>'id',
    'relationship_type'=>'one-to-one',
];
  1. Add link fields in both modules pointing to this relationship:
$dictionary['ModuleA']['fields']['moduleb_link'] = [
    'name'=>'moduleb_link','type'=>'link','relationship'=>'modulea_moduleb','module'=>'ModuleB','source'=>'non-db'
];

$dictionary['ModuleB']['fields']['modulea_link'] = [
    'name'=>'modulea_link','type'=>'link','relationship'=>'modulea_moduleb','module'=>'ModuleA','source'=>'non-db'
];
  1. Optional: Add relate fields for UI display using the links.
  2. Quick Repair & Rebuild in SuiteCRM.
2 Likes