Circular M:M relationship adds duplicate rows to the linking table

I'm trying to add a circular (relationship with itself) M:M relationship to the Product Templates module. I was to create it and the subsequent linking table and add the subpanels for both the left and right side of the relationship.

However, when I try to add/link/unlink records to any of the two subpanels, it is duplicated to the other. I checked the linking table, and 2 rows were added.

This is the vardefs for the relationship:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$dictionary["ProductTemplate"]["fields"]["producttemplates_producttemplates_1_left"] = array (
'name' => 'producttemplates_producttemplates_1_left',
'type' => 'link',
'relationship' => 'producttemplates_producttemplates_1',
'source' => 'non-db',
'module' => 'ProductTemplates',
'bean_name' => 'ProductTemplate',
'vname' => 'LBL_PRODUCTTEMPLATES_PRODUCTTEMPLATES_1_FROM_PRODUCTTEMPLATES_L_TITLE',
'id_name' => 'child_product_id',
'link-type' => 'many',
'side' => 'left',
);
$dictionary["ProductTemplate"]["fields"]["producttemplates_producttemplates_1_right"] = array (
'name' => 'producttemplates_producttemplates_1_right',
'type' => 'link',
'relationship' => 'producttemplates_producttemplates_1',
'source' => 'non-db',
'module' => 'ProductTemplates',
'bean_name' => 'ProductTemplate',
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


This is the metadata for the relationship:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
// created: 2020-07-28 19:01:14
$dictionary["producttemplates_producttemplates_1"] = array (
'true_relationship_type' => 'many-to-many',
'from_studio' => true,
'relationships' =>
array (
'producttemplates_producttemplates_1' =>
array (
'lhs_module' => 'ProductTemplates',
'lhs_table' => 'product_templates',
'lhs_key' => 'id',
'rhs_module' => 'ProductTemplates',
'rhs_table' => 'product_templates',
'rhs_key' => 'id',
'relationship_type' => 'many-to-many',
'join_table' => 'product_templates_product_templates_1_c',
'join_key_lhs' => 'parent_product_id',
'join_key_rhs' => 'child_product_id',
),
),
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

How can I make it so that, there should be no duplicates when I add/link a record to any of the subpanels?

EDIT:

After some digging in the Sugar Core files, I found out that when the you have a self referencing relationship (when LHS module = RHS module), it will do another addRow to the table. (M2MRelationship.php, line 142). This must be the one that's causing the additional row, but since this is a parent-child relationship, I would not need this.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
class M2MRelationship extends SugarRelationship {
...
//Many to many has no additional logic, so just add a new row to the table and notify the beans.
$dataToInsert = $this->getRowToInsert($lhs, $rhs, $additionalFields);
/**
* We need to do a complete check against all fields in the relationship to ensure that
* an update occurs for any additional relationship fields
* */
if (!$currentRow || !$this->compareRow($currentRow, $dataToInsert)) {
$this->addRow($dataToInsert);
if ($this->self_referencing) {
$this->addSelfReferencing($lhs, $rhs, $additionalFields); <---- This adds the additional row
}
...
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Any help is appreciated. Thanks!