One-to-One relationship in custom module: Two related ID fields?

Dear SuiteCRM community

I’m currently developing a custom module and would like to implement a One-2-One relationship. I’m wondering, whether it is actually necessary to have 2 related ID fields: One in Module A for the ID of module B and one in module B for the ID of module A? That does not make much sense from my perspective.

For One-2-Many relationships you have within the vardefs

  • one “relationship” entry
  • for each module a “link” entry
  • in the left side module (Many side) a “relate” field as well as the corresponding “id” field for the related
  • and in the right side module (One side) the subpanel definitions

For One-2-One relationships, however, I would assume that I can have a relate field in the right side module as well. However, the relate field does not work without the related ID field, which is not existent on the right side, because the one on the left side should be fully sufficient to define the one-2-one relationship.

I’ve tried the following:


$dictionary['Module_A'] = array(
	...
	'fields'=>array (
		'module_b_link_field' => 
		array (
			'name' => 'module_b_link_field',
			'type' => 'link',
			'relationship' => 'one-2-one_relationship_ab',
			'module' => 'Module_B',
			'bean_name' => 'Module_B',
			'source' => 'non-db',
		),
		'module_b_name_related' => 
		array (
			'name' => 'module_b_name_related',
			'vname' => 'LBL_MODULE_B_NAME',
			'type' => 'relate',
			'module' => 'Module_B',
			'link' => 'module_b_link_field',
//			'id_name' =>         For a one-to-many relationships, you would have the id field from the left side (many side) here. But on the right side for one-to-one relationships, it should not be necessary to have a second related ID field....
			'rname' => 'name',
			'dbType' => 'non-db',
			'source' => 'non-db',
			'required' => false,
		),
	),
	'relationships'=>array (
		'one-2-one_relationship_ab' => 
		array (
			'lhs_module' => 'Module_B',
			'lhs_table' => 'module_b',
			'lhs_key' => 'module_a_id',
			'rhs_module' => 'Module_A',
			'rhs_table' => 'module_a',
			'rhs_key' => 'id',
			'relationship_type' => 'one-to-one',
		),

But the relate field is not working. In module B everything works perfectly. And I’m sure, I would be able to create a subpanel in module A, if it would be a One-to-Many relationship. But I would just like to show the name of the one-to-one relationship within the Detailed View.

Do you have any idea, how to achieve this without custom programming in the view.detail.php files?
Many thanks

Hi,

I solved my problem and quickly wanted to share the solution. Two things are important to know and weren’t clear to me:

  1. The ‘id_name’ definition within the vardefs of a relate field does not only accept the name of the respective id field (as I assumed above), but also the name of the link field! The SugarBean class takes care of resolving the proper id field from the relationship.

  2. Left hand side and right hand side of the ‘one-to-one’ relationship are not the same!!! I thought that there would be no difference, but there is! You have to make sure that the module, which has the ID field for linking to the other module’s entry, is the right hand side (rhs) module within the relationship.
    Suitecrm is not able to resolve the one-to-one relationship on the right hand side module, if that side does not have the field for the id of the other module’s entry. However, if you switch rhs and lhs of the relationship everything works perfectly!

To make things short: A one-to-one relationship does not require id fields in both modules (which would be a replication of data), if you design the relationship in a way that the ONE id field is in the rhs module and if you put the lhs link field name in the ‘id_name’ property of the relate fields within the lhs module (because you won’t have a dedicated id filed on that left side).

Hope that this is helpful.

Best regards

2 Likes

I created one-to-one relationships with studio or builder several years ago.
Since then, my log is like this:
Warning: Multiple links found for relationship agr_quotations_agr_invoices_1 within module agr_Invoices
Warning: Multiple links found for relationship agr_quotations_agr_invoices_1 within module AGR_Quotations

How should I modify the generated code to make it proper and avoid the warnings ?

Thanks for anybody help.

Here are the codes:

$dictionary["agr_Invoices"]["fields"]["agr_quotations_agr_invoices_1"] = array (
  'name' => 'agr_quotations_agr_invoices_1',
  'type' => 'link',
  'relationship' => 'agr_quotations_agr_invoices_1',
  'source' => 'non-db',
  'module' => 'AGR_Quotations',
  'bean_name' => 'AGR_Quotations',
  'vname' => 'LBL_AGR_QUOTATIONS_AGR_INVOICES_1_FROM_AGR_QUOTATIONS_TITLE',
  'id_name' => 'agr_quotations_agr_invoices_1agr_quotations_ida',
);
$dictionary["agr_Invoices"]["fields"]["agr_quotations_agr_invoices_1_name"] = array (
  'name' => 'agr_quotations_agr_invoices_1_name',
  'type' => 'relate',
  'source' => 'non-db',
  'vname' => 'LBL_AGR_QUOTATIONS_AGR_INVOICES_1_FROM_AGR_QUOTATIONS_TITLE',
  'save' => true,
  'id_name' => 'agr_quotations_agr_invoices_1agr_quotations_ida',
  'link' => 'agr_quotations_agr_invoices_1',
  'table' => 'agr_quotations',
  'module' => 'AGR_Quotations',
  'rname' => 'name',
);
$dictionary["agr_Invoices"]["fields"]["agr_quotations_agr_invoices_1agr_quotations_ida"] = array (
  'name' => 'agr_quotations_agr_invoices_1agr_quotations_ida',
  'type' => 'link',
  'relationship' => 'agr_quotations_agr_invoices_1',
  'source' => 'non-db',
  'reportable' => false,
  'side' => 'right',
  'vname' => 'LBL_AGR_QUOTATIONS_AGR_INVOICES_1_FROM_AGR_QUOTATIONS_TITLE',
);
$dictionary["AGR_Quotations"]["fields"]["agr_quotations_agr_invoices_1"] = array (
  'name' => 'agr_quotations_agr_invoices_1',
  'type' => 'link',
  'relationship' => 'agr_quotations_agr_invoices_1',
  'source' => 'non-db',
  'module' => 'agr_Invoices',
  'bean_name' => 'agr_Invoices',
  'vname' => 'LBL_AGR_QUOTATIONS_AGR_INVOICES_1_FROM_AGR_INVOICES_TITLE',
  'id_name' => 'agr_quotations_agr_invoices_1agr_invoices_idb',
);
$dictionary["AGR_Quotations"]["fields"]["agr_quotations_agr_invoices_1_name"] = array (
  'name' => 'agr_quotations_agr_invoices_1_name',
  'type' => 'relate',
  'source' => 'non-db',
  'vname' => 'LBL_AGR_QUOTATIONS_AGR_INVOICES_1_FROM_AGR_INVOICES_TITLE',
  'save' => true,
  'id_name' => 'agr_quotations_agr_invoices_1agr_invoices_idb',
  'link' => 'agr_quotations_agr_invoices_1',
  'table' => 'agr_invoices',
  'module' => 'agr_Invoices',
  'rname' => 'document_name',
);
$dictionary["AGR_Quotations"]["fields"]["agr_quotations_agr_invoices_1agr_invoices_idb"] = array (
  'name' => 'agr_quotations_agr_invoices_1agr_invoices_idb',
  'type' => 'link',
  'relationship' => 'agr_quotations_agr_invoices_1',
  'source' => 'non-db',
  'reportable' => false,
  'side' => 'left',
  'vname' => 'LBL_AGR_QUOTATIONS_AGR_INVOICES_1_FROM_AGR_INVOICES_TITLE',
);
$dictionary["agr_quotations_agr_invoices_1"] = array (
  'true_relationship_type' => 'one-to-one',
  'from_studio' => true,
  'relationships' => 
  array (
    'agr_quotations_agr_invoices_1' => 
    array (
      'lhs_module' => 'AGR_Quotations',
      'lhs_table' => 'agr_quotations',
      'lhs_key' => 'id',
      'rhs_module' => 'agr_Invoices',
      'rhs_table' => 'agr_invoices',
      'rhs_key' => 'id',
      'relationship_type' => 'many-to-many',
      'join_table' => 'agr_quotations_agr_invoices_1_c',
      'join_key_lhs' => 'agr_quotations_agr_invoices_1agr_quotations_ida',
      'join_key_rhs' => 'agr_quotations_agr_invoices_1agr_invoices_idb',
    ),
  ),
  'table' => 'agr_quotations_agr_invoices_1_c',
  'fields' => 
  array (
    0 => 
    array (
      'name' => 'id',
      'type' => 'varchar',
      'len' => 36,
    ),
    1 => 
    array (
      'name' => 'date_modified',
      'type' => 'datetime',
    ),
    2 => 
    array (
      'name' => 'deleted',
      'type' => 'bool',
      'len' => '1',
      'default' => '0',
      'required' => true,
    ),
    3 => 
    array (
      'name' => 'agr_quotations_agr_invoices_1agr_quotations_ida',
      'type' => 'varchar',
      'len' => 36,
    ),
    4 => 
    array (
      'name' => 'agr_quotations_agr_invoices_1agr_invoices_idb',
      'type' => 'varchar',
      'len' => 36,
    ),
  ),
  'indices' => 
  array (
    0 => 
    array (
      'name' => 'agr_quotations_agr_invoices_1spk',
      'type' => 'primary',
      'fields' => 
      array (
        0 => 'id',
      ),
    ),
    1 => 
    array (
      'name' => 'agr_quotations_agr_invoices_1_ida1',
      'type' => 'index',
      'fields' => 
      array (
        0 => 'agr_quotations_agr_invoices_1agr_quotations_ida',
      ),
    ),
    2 => 
    array (
      'name' => 'agr_quotations_agr_invoices_1_idb2',
      'type' => 'index',
      'fields' => 
      array (
        0 => 'agr_quotations_agr_invoices_1agr_invoices_idb',
      ),
    ),
  ),
);