Problem with copying a bean with relationship.

I have an old scheduler that's been around for years that worked perfectly last year (v9.x) but is now broken on v11.0.2 (Professional On Site)

We only run it twice a year so I didn't notice until now.

In short I have a custom WContracts module that has a M-1 relationship with Accounts

(One Account on the WContract, many WContracts per Account)

This scheduler creates new WContract record for the subsequent contract period and it does so by copying the old Contract and changing some values for the new dates etc.

In essence:

      $newContractBean = BeanFactory::newBean('wcont_Contracts');
      //copy the contract fields
      foreach($oldContractBean->field_defs as $def){
          $field = $def['name'];
          $newContractBean->$field = $oldContractBean->$field;
      }
      unset($newContractBean->id);
      //make some other changes...
      $newContractBean->save();

This would correctly populate the New Contract including the related Account.

Now, suddenly, the related Account is blank.

Any ideas why the Account would not be populated?

The vardefs clearly show that WContracts has the fields for the account link (type = link), the account name (type = relate) and the account id (type = id).

What has changed in v11 that would make this stop working?

Thanks,

FrancescaS

-- Edit:

Further investigation shows that after saving the bean the id for the Account is there, the relationship can be retrieved and the id can be printed ( did a loop just to be sure, and yes, there is only one);

      $link_account = 'wcont_wcontracts_accounts';
      if($new_cont->load_relationship($link_account)){
         foreach($new_cont->$link_account->getBeans() as $related_account){
           $GLOBALS['log']->fatal('related account id ' . $related_account->id);
         }
      }

The account id is indeed the correct one, the account exists and is not deleted.

But when I query the tables the relationship table has no records for that contract

select * from wcont_wcontracts_accounts_c wca 
where wcont_wcontracts_accountswcont_wcontracts_idb  = '5fb81504-58fb-11ec-93bc-001a4a16020e'

returns nothing.

I turned on the DEBUG on my logs and I can see the relationship being processed:

1160 Thu Dec  9 09:39:18 2021 [21325][1][INFO] Entered operation status: updating_relationships
1161 Thu Dec  9 09:39:18 2021 [21325][1][DEBUG] SugarBean[wcont_WContracts].load_relationships, Loading link (wcont_wcontracts_accounts).
1162 Thu Dec  9 09:39:18 2021 [21325][1][DEBUG] save_relationship_changes(): From field_defs - attempting to add a relationship record - wcont_wcontracts_accounts = 6bf3ba81-679d-70ee-c02e-4edfebb2a10a
1163 Thu Dec  9 09:39:18 2021 [21325][1][DEBUG] save_relationship_changes(): From field_defs - add a relationship record returned array (
1164   0 => '6bf3ba81-679d-70ee-c02e-4edfebb2a10a',

And there are no errors in the sugar or php logs.